# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## 2020년 4월 13일 월요일
# ### Programmers - 스택 / 큐 :  다리를 지나는 트럭
# ### 문제 : https://programmers.co.kr/learn/courses/30/lessons/42583
# ### 블로그 : https://somjang.tistory.com/entry/Programmers-%EC%8A%A4%ED%83%9D%ED%81%90-%EB%8B%A4%EB%A6%AC%EB%A5%BC-%EC%A7%80%EB%82%98%EB%8A%94-%ED%8A%B8%EB%9F%AD-Python

# ### 첫번째 시도

# +
from collections import deque

def solution(bridge_length, weight, truck_weights):
    answer = 0

    truck_weights_deque = deque(truck_weights)
    
    trucks_on_bridge = []
    
    while True:
        if len(truck_weights_deque) == 0:
            break
        if sum(trucks_on_bridge) < weight:
            trucks_on_bridge.append(truck_weights_deque.popleft())
            if len(truck_weights_deque) == 0:
                answer = answer + bridge_length
                
        elif sum(trucks_on_bridge) >= weight:
            answer = answer + bridge_length
            trucks_on_bridge = []
            
    answer = answer + len(truck_weights)
    
    return answer


# -

# ---

# ### 두번째 시도

# +
from collections import deque

def solution(bridge_length, weight, truck_weights):
    answer = 1
    
    truck_weights_deque = deque(truck_weights)
    
    trucks_on_bridge_deque = deque([0] * bridge_length)
    
    trucks_on_bridge_deque[-1] = truck_weights_deque.popleft()
    
    while truck_weights_deque:
        if sum(trucks_on_bridge_deque) - trucks_on_bridge_deque[0] + truck_weights_deque[0] > weight:
            trucks_on_bridge_deque.popleft()
            trucks_on_bridge_deque.append(0)
            answer = answer + 1
        else:
            trucks_on_bridge_deque.popleft()
            trucks_on_bridge_deque.append(truck_weights_deque.popleft())
            answer = answer + 1
    answer = answer + bridge_length
    
    return answer

StarcoderdataJupyterScriptsDedupFiltered