안녕하세요 :)
https://programmers.co.kr/learn/courses/30/lessons/42583
5번 테케 시간초과 나는거 때문에 여러 수정을 거쳐서 통과했습니다..
weight sum을 구하는 부분을 처음에 배열로 했다가 그냥 연산해서 처리하도록 int로 바꿨구요.
current_bridge[:-1] 로 풀면 시간초과가 나더라구요.
그래서 current_bridge.pop()으로 바꿔서 풀었습니다 .
def solution(bridge_length, weight, truck_weights):
answer = 0
truck_weights.reverse()
current_bridge = [0] * bridge_length
current_bridge[0] = truck_weights.pop()
weight_sum = current_bridge[0]
while truck_weights:
answer += 1
if truck_weights[-1] + weight_sum - current_bridge[-1] <= weight:
item = truck_weights.pop()
weight_sum += item
weight_sum -= current_bridge[-1]
current_bridge.pop()
current_bridge = [item] + current_bridge
elif weight_sum == 0:
item = truck_weights.pop()
weight_sum -= current_bridge[-1]
weight_sum += item
current_bridge[-1] = 0
current_bridge[0] = item
else:
weight_sum -= current_bridge[-1]
current_bridge.pop()
current_bridge = [0] + current_bridge
return answer + bridge_length + 1