본문 바로가기
algorithm/coding test

프로그래머스 - 다리를지나는트럭 (파이썬)

by su0a 2024. 4. 15.

list를 que처럼 사용하여 풀기


def solution(bridge_length, weight, truck_weights):
    answer = 0
    tot=0
    que_i=0
    que=[0]*bridge_length
    for truck_weight in truck_weights:
        tot-=que.pop(0) #맨 앞에 que값 다리를 건넘
        while(tot+truck_weight>weight): #다리에 있는 트럭 무게가 weight보다 클경우
            que.append(0) #0을 추가시키고
            answer+=1
            tot-=que.pop(0) #맨 앞에 que값 빼기
        tot+=truck_weight
        que.append(truck_weight) #que맨뒤에 트럭의 무게 넣기
        answer+=1
    answer+=bridge_length #맨 뒤의 truck_weight값 다리를 지나기 위해 걸리는 시간
    return answer