Scribbling

프로그래머스: 다리를 지나는 트럭 본문

Computer Science/Coding Test

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

focalpoint 2021. 10. 15. 00:21
from collections import deque
def solution(bridge_length, weight, truck_weights):
    time, total_weight = 1, 0
    q = deque()
    truck_weights = deque(truck_weights)
    while truck_weights:
        if not q:
            w = truck_weights.popleft()
            q.append([time, w])
            total_weight += w
        else:
            if q[0][0] + bridge_length == time:
                total_weight -= q[0][1]
                q.popleft()
            w = truck_weights[0]
            if total_weight + w <= weight:
                w = truck_weights.popleft()
                q.append([time, w])
                total_weight += w
        time += 1
    return q[-1][0] + bridge_length