728x90
<문제 링크>
https://programmers.co.kr/learn/courses/30/lessons/42583
코딩테스트 연습 - 다리를 지나는 트럭
트럭 여러 대가 강을 가로지르는 일차선 다리를 정해진 순으로 건너려 합니다. 모든 트럭이 다리를 건너려면 최소 몇 초가 걸리는지 알아내야 합니다. 다리에는 트럭이 최대 bridge_length대 올라갈
programmers.co.kr
<문제 풀이>
- Queue 사용
<코드>
def solution(bridge_length, weight, truck_weights):
Q = [0] * bridge_length
total_weight = 0
time = 0
while truck_weights:
total_weight -= Q.pop(0)
if total_weight + truck_weights[0] <= weight:
Q.append(truck_weights[0])
total_weight += truck_weights.pop(0)
else:
Q.append(0)
time += 1
time += bridge_length
return time
<고쳐야 할 점>
- sum은 메모리를 많이 잡아먹음
- <if 리스트:> 문법 기억하기
- while에 뭐가 들어갈지, time은 어떻게 할지 생각
'두두의 알고리즘 > 문제' 카테고리의 다른 글
[완전탐색] 프로그래머스 L2 '소수 찾기' (Python) (0) | 2021.11.22 |
---|---|
[완전탐색] 프로그래머스 L1 '모의고사' (Python) (0) | 2021.11.22 |
[큐] 프로그래머스 L2 '기능개발' (Python) (0) | 2021.11.22 |
[스택] 프로그래머스 L2 '주식가격' (Python) (0) | 2021.11.22 |
[탐욕법] 프로그래머스 L1 '체육복' (Python) (0) | 2021.11.22 |