반응형

 

출처: 프로그래머스 코딩 테스트 연습, 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
반응형

+ Recent posts