관리 메뉴

솜씨좋은장씨

[Programmers] 스택/큐 : 다리를 지나는 트럭 (Python) 본문

Programming/코딩 1일 1문제

[Programmers] 스택/큐 : 다리를 지나는 트럭 (Python)

솜씨좋은장씨 2020. 4. 13. 00:21
728x90
반응형

1일 1문제 67일차!

오늘의 문제는 프로그래머스의 다리를 지나는 트럭입니다.

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

첫번째 시도

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

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

 

Comments