알고리즘/프로그래머스

[알고리즘 고득점 kit/스택, 큐] 다리를 지나는 트럭

_jju 2026. 4. 29. 19:56

from collections import deque

def solution(bridge_length, weight, truck_weights):
    answer = 0
    dq = deque(0 for i in range(bridge_length))
    truck = deque(truck_weights)
    
    sum = 0
    t = 0
    result = []
    
    while result != truck_weights:
        n = dq.popleft()
        sum -= n
        result.append(n)
        if sum + truck[0] <= weight:
            sum += truck[0]
            dq.append(truck.popleft())
        else:
            dq.append(0)
        
    return answer

맨 앞에서 pop을 하고 그 값을 result에 넣어준 뒤, 맨 뒤에서 대기하고 있는 버스들을 넣어주는 방식으로 구현했다. result가 초기 대기열이랑 같아지면 멈추고 시간을 반환한다.