반응형
programmers.co.kr/learn/courses/30/lessons/42583
처음엔 deque없이 pop(0)을 썼다가 시간초과가 떠서 deque를 사용하여 popleft()를 썼다.
import collections
def solution(bridge_length, weight, truck_weights):
trucks = collections.deque(truck_weights)
queue = collections.deque([0]*bridge_length)
t = 0
cur = 0 # current weight on bridge
while True:
if not trucks:
t += bridge_length
break
cur -= queue.popleft()
if cur + trucks[0] <= weight:
queue.append(trucks.popleft())
cur += queue[-1]
else:
queue.append(0)
t +=1
return t
댓글