https://programmers.co.kr/learn/courses/30/lessons/42583
✅ Solution
- [0] * bridge_length로 bridge 배열을 만들어준다.
- bridge 배열의 길이가 0보다 클 때,
- 맨 앞의 요소를 bridge배열에서 popleft(), total_weight에서 빼주고 answer에 1을 더해준다.
- 위 과정은 맨 앞의 요소를 빼주면서 한 칸씩 앞으로 전진시키는 것
- 대기 트럭이 있고 (해당 트럭의 무게 + total_weight)가 weight 보다
- 작을 때, 대기 트럭 맨 앞의 요소를 bridge의 맨 뒤에 넣어주고 total_weight에 트럭의 무게를 더해준다.
- 클 때, 맨 뒤에 0을 넣어준다.
✅ Code
from collections import deque
def solution(bridge_length, weight, truck_weights):
answer = 0
total_weight = 0
bridge = deque([0] * bridge_length)
truck_weights = deque(truck_weights)
while bridge:
answer += 1
total_weight -= bridge.popleft()
if truck_weights:
if truck_weights[0] + total_weight <= weight:
total_weight += truck_weights[0]
bridge.append(truck_weights.popleft())
else:
bridge.append(0)
return answer
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] Level 2 - 피로도 (Python) (0) | 2022.06.23 |
---|---|
[프로그래머스] Level 2 - 2개 이하로 다른 비트 (Python) (0) | 2022.06.23 |
[프로그래머스] Level 2 - 위장 (Python) (0) | 2022.06.09 |
[프로그래머스] Level 2 - 게임 맵 최단거리 (Python) (0) | 2022.06.09 |
[프로그래머스] Level 2 - 배달 (Python) (0) | 2022.06.09 |