https://school.programmers.co.kr/learn/courses/30/lessons/42583
나의 풀이 (오답)
from collections import deque
def solution(bridge_length, weight, truck_weights):
q=deque()
q.append(truck_weights[0])
time=1
truck_weights = truck_weights[1:]
while truck_weights:
if len(q)<bridge_length and sum(q)+truck_weights[0]<=weight:
q.append(truck_weights[0])
truck_weights.pop(0)
time+=1
else:
q.popleft()
time+=1
return time
모범 답안
from collections import deque
def solution(bridge_length, weight, truck_weights):
second=0
current_weight=0
bridge = deque([0] * bridge_length)
while bridge:
current_weight -= bridge.popleft()
second +=1
if truck_weights:
if current_weight + truck_weights[0] <= weight:
truck = truck_weights.pop(0)
bridge.append(truck)
current_weight += truck
else:
bridge.append(0)
return second
'코딩테스트 > programmers (python)' 카테고리의 다른 글
Programmers / 2단계 / 방문 길이 / python (1) | 2024.03.06 |
---|---|
Programmers / 2단계 / [3차] n진수 게임 / python / 2018 KAKAO BLIND RECRUITMENT (1) | 2024.03.05 |
Programmers / 2단계 / [3차] 압축 / python / 2018 KAKAO BLIND RECRUITMENT (0) | 2024.02.23 |
Programmers / 2단계 / 땅따먹기 / python (0) | 2024.02.21 |
Programmers / 2단계 / 뒤에 있는 큰 수 찾기 / python (0) | 2024.02.21 |