본문 바로가기
프로그래머스/1단계

(Python/LV1)햄버거 만들기

by windy7271 2022. 10. 30.
728x90
반응형

문제 출처:

 

풀이:

 

첫 시도

def solution(ingredient):

    full =''
    for i in ingredient:
        full = full + str(i)
    count = 0
    while '1231' in full:
        if '1231' in full:
            count += 1
            full = full.replace('1231','')
    return count
print(solution([2, 1, 1, 2, 3, 1, 2, 3, 1]))

41.2 / 100  코드

스택을 써 보기로함.

 

stack = []
count = 0

for i in ingredient:
    stack.append(i) # 일단넣는다
    if stack[-4:] == [1,2,3,1]: # 1,2,3,1 빵야채고기빵 이 나오면 
        count += 1	# 카운트 1개 올리고
        for i in range(4): 	# 1,3,2,1 순서로 빼줌
            stack.pop()
return count

 

반응형

댓글