L2 : 다리를 지나는 트럭 Python

jhyunn·2023년 1월 16일
0

Programmers

목록 보기
28/69

L2 : 다리를 지나는 트럭 Python

https://school.programmers.co.kr/learn/courses/30/lessons/42583

def solution(bridge_length, weight, truck_weights):
    on_bridge_truck, on_bridge_time = [], []
    current_weight, truck, cnt = 0, 0, 0
    
    while 1:
        cnt += 1
        on_bridge_time = [t+1 for t in on_bridge_time]
        
        # 맨 앞 트럭이 다리 끝에 도착
        try:
            if on_bridge_time[0] == bridge_length:
                current_weight -= truck_weights[on_bridge_truck[0]]
                on_bridge_truck.remove(on_bridge_truck[0])
                on_bridge_time.remove(on_bridge_time[0])
        except:
            pass
        
        # 트럭이 추가되어도 무게를 초과하지 않는 경우
        try:
            if current_weight+truck_weights[truck]<=weight:
                on_bridge_truck.append(truck)
                on_bridge_time.append(0)
                current_weight += truck_weights[truck]
                truck += 1
        except:
            pass
        
        if len(on_bridge_truck) == 0:
            return cnt

dict, numpy, deque 등 방법은 다양하다.

#remove

profile
https://github.com/Sungjeonghyun

0개의 댓글