일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- import re
- 코테
- 연습문제
- sql
- 코딩
- 파이썬
- 데이터분석
- counter
- collections
- coding test
- 완전탐색
- 조합
- 시간복잡도
- join
- lv4
- Level2
- 코딩테스트
- BFS
- Queue
- 프로그래머스
- programmers
- mysql
- itertools
- level4
- coding
- CodingTest
- python
- Stack
- time complexity
- lambda
- Today
- Total
ror_coding
[Programmers] 햄버거 만들기 - 133502 본문
728x90
시간 초과로 stack 이용해서 O(n)로 풀기.
Question
상수에게 전해지는 재료의 정보를 나타내는 정수 배열 ingredient가 주어졌을 때, 상수가 포장하는 햄버거의 개수를 return 하도록 solution 함수를 완성하시오.
Code : Time Complexity O(n)
def solution(ingredient):
answer = 0
stack = []
for i in ingredient: # O(n)
stack.append(i) # O(1)
if stack[-4:] == [1,2,3,1] : answer += 1; del stack[-4:] # O(1)
return answer
Code 2 : Time Complexity O(n^2)
def solution(ingredient):
answer = 0
hamberger = "1231"
str_ing = ''.join(map(str,ingredient))
while hamberger in str_ing : # in O(n)
answer += 1
str_ing = str_ing.replace(hamberger,'',1) # O(n)
return answer
![](https://t1.daumcdn.net/keditor/emoticon/friends1/large/043.gif)
now me
On my github
728x90
'Algorithm > Python' 카테고리의 다른 글
[Programmers] 귤 고르기 - 138476 (0) | 2024.10.24 |
---|---|
[Programmers] 구명보트 - 42885 (1) | 2024.10.24 |
[Programmers] 공원 산책 - 172928 (3) | 2024.10.23 |
[Programmers] 숫자 짝꿍 - 131128 (0) | 2024.10.22 |
[Programmers] 체육복 - 42862 (1) | 2024.10.17 |