문제 제목 : 다리를 지나는 트럭
문제 출처 : 프로그래머스
알고리즘 : Queue(Algorithms & DataStructures)
문제원본링크
https://school.programmers.co.kr/learn/courses/30/lessons/42583
from collections import defaultdict, deque
def solution(bridge_length, weight, truck_weights):
ans, rest_truck, bridge_sum = 0, len(truck_weights), 0
bridge = deque([0]*bridge_length)
while truck_weights or rest_truck:
ans += 1
if bridge[0]: rest_truck -= 1
bridge_sum -= bridge.popleft()
next_car = 0
if truck_weights and bridge_sum + truck_weights[0] <= weight:
next_car = truck_weights[0]
del truck_weights[0]
bridge.append(next_car)
bridge_sum += next_car
return ans
'Problem Solving' 카테고리의 다른 글
[Math] 점프와 순간이동-프로그래머스 (0) | 2022.10.12 |
---|---|
[Simulation] 교차로-소프티어(별3) (0) | 2022.09.04 |
[Brute Force] 카펫-프로그래머스(Lv.2) (0) | 2022.07.24 |
[Brute Force] 소수 찾기-프로그래머스(Lv.2) (0) | 2022.07.24 |
[Brute Force] 모의고사-programmers(Lv.1) (0) | 2022.07.24 |