이 문제 설명이 참 애매했어서 헤맸는데,
풀이는 다음과 같습니다.
다리(queue)에 있는 트럭을 하나씩 빼고,
다리의 하중 여유가 된다면 트럭을 추가하는 방식으로 했습니다,
만약 다리의 하중 여유가 되지 않는다면(total + orders[0] > weight), 무게가 0짜리인 트럭을 넣어줬고요(append).
만약 다리의 하중 여유가 된다면, total에는 w를 더해주고, 다리(queue)에는 w를 넣어줬습니다(append).
from collections import deque
def solution(bridge_length, weight, truck_weights):
queue = deque([0]*bridge_length)
orders = deque(truck_weights)
time=0
total=0
while orders:
time+=1
total -= queue[0]
queue.popleft()
if total + orders[0] > weight:
queue.append(0)
else:
w = orders.popleft()
total+=w
queue.append(w)
return time+bridge_length
너무 깔끔해서 감명받았습니다.