반응형
문제
https://school.programmers.co.kr/learn/courses/30/lessons/42583
해설
두개의 큐(현재 코드에선 리스트로 구현)를 활용하여 해결할 수 있는 문제였다.
def solution(bridge_length, weight, truck_weights):
second = 0
on_bridge_car = []
on_bridge_sec = []
while truck_weights or on_bridge_car:
second += 1
pass_state = False
for i in range(len(on_bridge_car)):
if on_bridge_sec[i] < bridge_length:
on_bridge_sec[i] += 1
if on_bridge_sec[i] == bridge_length:
pass_state = True
if pass_state:
on_bridge_car.pop(0)
on_bridge_sec.pop(0)
if truck_weights and sum(on_bridge_car)+truck_weights[0] <= weight:
on_bridge_car.append(truck_weights.pop(0))
on_bridge_sec.append(0)
return second
반응형