코딩테스트/프로그래머스

[프로그래머스][스택] 택배상자

박소민 2024. 10. 31. 01:31
택배상자
 

프로그래머스

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

programmers.co.kr

 

  • 내 풀이
    • 앞, 뒤의 값들과 비교하는 경우 스택문제인걸 고려해봐야함
def solution(order):
    stack=[i for i in range(1,order[0]+1)]
    idx=0
    cur=order[0]
    while cur<=len(order) and idx<=len(order):
        if not stack:
            cur+=1
            stack.append(cur)
            continue
        
        if stack[-1]>=order[idx]:
            if stack[-1]-order[idx]>=1:
                break
            else:
                stack.pop()
                idx+=1
        else:
            cur+=1
            stack.append(cur)
            continue
            
    
    return idx