Search

햄버거 만들기

입출력 예시

나의 코드

def solution(ingredient): count=0 i = 0 while i < len(ingredient): if ingredient[i:i+4] == [1, 2, 3, 1]: del ingredient[i:i+4] count+=1 i -= 2 else: i += 1 return count
Python
복사
# failed solution (시간초과) def solution(ingredient): st = ('').join(map(str, ingredient)) count = 0 while True: print(st) ind = st.find('1231') if ind == -1: break st = st[:ind] + st[ind+4:] count += 1 return count
Python
복사

다른 풀이

def solution(ingredient): s = [] cnt = 0 for i in ingredient: s.append(i) if s[-4:] == [1, 2, 3, 1]: cnt += 1 for i in range(4): s.pop() return cnt
Python
복사

개선점 분석

처음 #failed solution 을 시도했었는데, string으로 slicing을 통해서 문제를 해결하려고 했음.
틀리지는 않았는데 slicing 과정이 time complexity가 높은 것 같음.
그래서 list로, del 을 사용해 해당부분만 지워서 해결.
다른 풀이를 보면, 하나씩 새로운 list에 추가하면서 마지막 부분이 1,2,3,1에 해당하면 pop 하는 식으로 수행함.