- 시간을 1초씩 증가시키면서 매순간 큐에서 나가야하는 트럭은 빼주고, 트럭이 들어올 수 있으면 큐에 넣어주는 작업을 반복하면 된다.
- 종료조건은 모든 트럭을 큐에 넣었으며, 큐가 비어있는 경우에만(다리를 모두 건넜을 때만) 종료하도록 함.
from collections import deque
def solution(bridge_length, weight, truck_weights):
answer = 0
idx = 0
cur_weight = 0
time = 0
queue = deque()
while True:
if queue and queue[0][1] <= time:
cur_weight = cur_weight - queue[0][0]
queue.popleft()
if idx < len(truck_weights) and weight >= cur_weight + truck_weights[idx]:
queue.append([truck_weights[idx], time+bridge_length])
cur_weight = cur_weight + truck_weights[idx]
idx += 1
time += 1
if idx == len(truck_weights) and not queue:
break
answer = time
return answer
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
'알고리즘[Python] > 프로그래머스' 카테고리의 다른 글
[ Lv 2 ] 영어 끝말잇기 (0) | 2021.06.27 |
---|---|
[ Lv 2 ] 구명보트 (0) | 2021.06.27 |
[ Lv 2 ] 카펫 (0) | 2021.06.26 |
[ Lv 2 ] 위장 (0) | 2021.06.26 |
[ Lv 2 ] 큰 수 만들기 (0) | 2021.06.25 |