honey_pot

[python] 다리를 지나는 트럭 - queue 본문

문제 풀이

[python] 다리를 지나는 트럭 - queue

_tera_ 2021. 11. 6. 17:22

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

 

def solution(bridge_length, weight, truck_weights):
    
    queue = [0] * bridge_length # 빈 다리
    time = 0 # 시간
    
    while queue: # queue가 빌 때(false)가 될때까지
        time += 1 # 시간 +1
        queue.pop(0) # 맨 앞 빼서 트럭 들어갈 자리 만듦
        
        if truck_weights: # 남은 트럭이 없을 때까지
            if sum(queue) + truck_weights[0] <= weight: # 다리 위 무게에 들어갈 트럭 무게 더하면 하중을 -> 넘지 않음
                queue.append(truck_weights.pop(0)) # 맨 뒤에 해당 트럭 붙임
            else: # 넘음
                queue.append(0) # 빈 자리 추가
            
    return time

queue의 길이만큼 루프를 수행하고 트럭무게에도 true/false if 조건문을 주는게 포인트

'문제 풀이' 카테고리의 다른 글

[python] 문자열 압축  (0) 2021.11.09
[python] 카펫  (0) 2021.11.07
[python] 프린터 - queue, any  (0) 2021.11.06
[python] 위장 (Counter, reduce 함수)  (0) 2021.11.04
[python] 피보나치의 수  (0) 2021.09.14
Comments