[프로그래머스] 다리를 지나는 트럭

 

문제

from collections import deque

def solution(bridge_length, weight, truck_weights):
    answer = 0
    finish = deque()
    ing = deque()
    n = len(truck_weights)
    
    tot_weight = 0
    while len(finish) < n:
        answer+=1
        
        if ing and ing[0][1] + bridge_length == answer:
            k = ing.popleft()
            finish.append(k[0])
            tot_weight -= k[0]
            
        if truck_weights and tot_weight + truck_weights[0] <= weight:
            l = truck_weights.pop(0)
            ing.append([l, answer])
            tot_weight += l
    return answer