프로그래머스/LV2

[프로그래머스 Lv2,python]- 택배상자

chyam_eun 2025. 1. 26. 12:56

https://school.programmers.co.kr/learn/courses/30/lessons/131704

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

from collections import deque
def solution(order):
    cnt = 0 # 가능한 택배 수
    stack=[] # 컨베이어 벨트
    num=1 # 크기
    order=deque(order) # queue로 표현
    while(len(order)>0): # order의 길이가 1이상일때 동안
        if order[0]>num: # order의 0번 인덱스가 num보다 크면 stack에 추가해주기
            stack.append(num)
            num+=1
        elif order[0]==num: # 같으면 택배를 싣을수 있으므로 수 증가하고 order에서 꺼내기
            cnt+=1
            order.popleft()
            num+=1
        else: # num이 클때
            if stack[-1]==order[0]: # 스택의 마지막과 같으면 증가시키고 꺼내기
                cnt+=1
                stack.pop()
                order.popleft()
            else:
                break
    return cnt

처음에 deque를 사용하지 않고 order.pop(0)을 했더니 시간초과가 걸려서 바꿔주었다.