importcollectionsfromtypingimportListdefsolution(bridge_length:int,weight:int,truck_weights:List[int])->int:answer=0dq=collections.deque([0]*bridge_length)# 다리 위의 현재의 무게
current_weight=0# 모든 트럭이 다리를 건너가고, 다리가 비어 있을 때까지 반복
whiledq:# 매시간(answer)마다
# 다리의 맨 앞에 있는 트럭(트럭이 없다면 0)이 다리를 빠져 나옴
answer+=1current_weight-=dq.popleft()# 만약 대기 중인 트럭이 있다면
iftruck_weights:# 다음 트럭이 다리에 진입할 수 있는지 확인
# 다음 트럭을 추가해도 다리 무게 한도를 초과하지 않으면 진입 가능
ifcurrent_weight+truck_weights[0]<=weight:# 대기열에서 맨 앞 트럭을 제거 후, 다리에 추가
next_truck=truck_weights.pop(0)dq.append(next_truck)current_weight+=next_truckcontinue# 트럭이 다리에 진입할 수 없다면
# dq 안에 0이라는 것은 트럭이 없다는 것을 의미를 추가
dq.append(0)returnanswerprint(solution(2,10,[7,4,5,6]))print(solution(100,100,[10]))print(solution(100,100,[10,10,10,10,10,10,10,10,10,10]))
Leave a comment