반응형
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
테스트 케이스 한 곳에서 계속 시간초과 에러 남
def solution(bridge_length, weight, truck_weights):
answer = 0
truck_weights.reverse()
bridge = [0 for i in range(bridge_length)]
while bridge:
answer += 1
bridge.pop(0)
if truck_weights:
sum_weights = sum(bridge)
if sum_weights + truck_weights[-1] > weight:
bridge.append(0)
else:
bridge.append(truck_weights.pop())
return answer
sum 때문에 시간초과 나서 바꿨다
def solution(bridge_length, weight, truck_weights):
answer = 0
truck_weights.reverse()
bridge = [0 for i in range(bridge_length)]
sum_weights = sum(bridge)
while bridge:
answer += 1
off = bridge.pop(0)
sum_weights -= off
if truck_weights:
if sum_weights + truck_weights[-1] > weight:
bridge.append(0)
else:
on = truck_weights.pop()
bridge.append(on)
sum_weights += on
return answer
def solution(bridge_length, weight, truck_weights):
answer = 0
bridge = [0 for i in range(bridge_length)]
while bridge:
answer += 1
bridge.pop(0)
if truck_weights:
sum_weights = sum(bridge)
if sum_weights + truck_weights[0] > weight:
bridge.append(0)
else:
bridge.append(truck_weights.pop(0))
return answer
반응형
'프로그래머스 > level 2' 카테고리의 다른 글
[프로그래머스] 카펫 (0) | 2022.07.07 |
---|---|
[프로그래머스] H-Index (0) | 2022.06.30 |
[프로그래머스] 기능개발 (0) | 2022.05.25 |
[프로그래머스] 행렬 테두리 회전하기 (0) | 2022.05.12 |
[프로그래머스] 거리두기 확인하기 (0) | 2022.05.12 |