카테고리 없음

[프로그래머스, python3] 다리를 지나는 트럭(deque)

전두선 2021. 1. 20. 12:52
[프로그래머스, python3] 다리를 지나는 트럭
난이도 Level 2
분류 스택/큐
programmers.co.kr/learn/courses/30/lessons/42583

 


문제 설명

트럭 여러 대가 강을 가로지르는 일 차선 다리를 정해진 순으로 건너려 합니다. 모든 트럭이 다리를 건너려면 최소 몇 초가 걸리는지 알아내야 합니다. 트럭은 1초에 1만큼 움직이며, 다리 길이는 bridge_length이고 다리는 무게 weight까지 견딥니다.
※ 트럭이 다리에 완전히 오르지 않은 경우, 이 트럭의 무게는 고려하지 않습니다.

예를 들어, 길이가 2이고 10kg 무게를 견디는 다리가 있습니다. 무게가 [7, 4, 5, 6]kg인 트럭이 순서대로 최단 시간 안에 다리를 건너려면 다음과 같이 건너야 합니다.

경과 시간 다리를 지난 트럭 다리를 건너는 트럭 대기 트럭
0 [] [] [7,4,5,6]
1~2 [] [7] [4,5,6]
3 [7] [4] [5,6]
4 [7] [4,5] [6]
5 [7,4] [5] [6]
6~7 [7,4,5] [6] []
8 [7,4,5,6] [] []

따라서, 모든 트럭이 다리를 지나려면 최소 8초가 걸립니다.

solution 함수의 매개변수로 다리 길이 bridge_length, 다리가 견딜 수 있는 무게 weight, 트럭별 무게 truck_weights가 주어집니다. 이때 모든 트럭이 다리를 건너려면 최소 몇 초가 걸리는지 return 하도록 solution 함수를 완성하세요.

제한 조건

  • bridge_length는 1 이상 10,000 이하입니다.
  • weight는 1 이상 10,000 이하입니다.
  • truck_weights의 길이는 1 이상 10,000 이하입니다.
  • 모든 트럭의 무게는 1 이상 weight 이하입니다.

 

입출력 예

bridge_length weight truck_weights return
2 10 [7,4,5,6] 8
100 100 [10] 101
100 100 [10,10,10,10,10,10,10,10,10,10] 110

소스코드 

  • deque를 활용
  • bridge_state_dQ의 경우에는 bridge_length만큼의 크기를 가지며 각각의 위치에 트럭이 있는지 없는지에 대한 상태를 나타낸다. (0: 트럭이 존재하지 않음, >0: 트럭이 존재
  • bridge로 들어오는 트럭 무게를 견딜 수 있는지 없는지를 파악
    • (bridge의 총 트럭무게 - bridge에서 나가는 트럭무게 + bridge로 들어오는 트럭무게) > weight
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from collections import deque
 
def solution(bridge_length, weight, truck_weights):
    time = 1
    
    truck_weights_dQ = deque(truck_weights)
    bridge_state_dQ = deque([0* bridge_length)
    
    truck_in = truck_weights_dQ.popleft()
    bridge_state_dQ[-1= truck_in
    weight_sum = truck_in
    while truck_weights_dQ:
        if weight_sum - bridge_state_dQ[0+ truck_weights_dQ[0> weight:
            truck_out = bridge_state_dQ.popleft()
            bridge_state_dQ.append(0)
            weight_sum -= truck_out
            time = time + 1
        else:
            truck_out = bridge_state_dQ.popleft()
            truck_in = truck_weights_dQ.popleft()
            bridge_state_dQ.append(truck_in)
            weight_sum = weight_sum - truck_out + truck_in
            time = time + 1
    time = time + bridge_length
 
    return time

 


분석

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from collections import deque
 
def solution(bridge_length, weight, truck_weights):
    time = 1
 
    truck_weights_dQ = deque(truck_weights)
    bridge_state_dQ = deque([0* bridge_length)
    print(f'truck_weights_dQ: {truck_weights_dQ}')
    print(f'bridge_state_dQ: {bridge_state_dQ}')
 
    truck_in = truck_weights_dQ.popleft()
    bridge_state_dQ[-1= truck_in
    weight_sum = truck_in
    while truck_weights_dQ:
        print(f'bridge_state_dQ: {bridge_state_dQ}')
        print(f'time: {time}, weight_sum: {weight_sum}, bridge_state_dQ[0]: {bridge_state_dQ[0]}, truck_weights_dQ[0]: {truck_weights_dQ[0]} ')
        print(f'{weight_sum - bridge_state_dQ[0] + truck_weights_dQ[0] > weight:}')
        print('-' * 60)
        if weight_sum - bridge_state_dQ[0+ truck_weights_dQ[0> weight:
            truck_out = bridge_state_dQ.popleft()
            bridge_state_dQ.append(0)
            weight_sum -= truck_out
            time = time + 1
        else:
            truck_out = bridge_state_dQ.popleft()
            truck_in = truck_weights_dQ.popleft()
            bridge_state_dQ.append(truck_in)
            weight_sum = weight_sum - truck_out + truck_in
            time = time + 1
    print(f'time: {time}, bridge_length: {bridge_length}')
    time = time + bridge_length
 
    return time
 
if __name__ == "__main__":
    print('-' * 40)
 
    case = 3
    bridge_length = [2,100,100]
    weight     = [10,100,100]
    truck_weights = [[7,4,5,6],[10],[10,10,10,10,10,10,10,10,10,10]]
    correct_answer = [8,101,110]
 
    for i in range(case):
        print(f"<<<<<  case {i}  >>>>>")
        answer = solution(bridge_length[i],weight[i],truck_weights[i])
        print('answer :' , answer, '/ correct_answer :' , correct_answer[i])
        if correct_answer[i] == answer:
            print(f'테스트 {i}를 통과하였습니다.')
        else:
            print(f'테스트 {i}를 통과하지 못했습니다.')
        print('-' * 40)
 
 
 
 
----------------------------------------
<<<<<  case 0  >>>>>
truck_weights_dQ: deque([7456])
bridge_state_dQ: deque([00])
bridge_state_dQ: deque([07])
time: 1, weight_sum: 7, bridge_state_dQ[0]: 0, truck_weights_dQ[0]: 4 
True
------------------------------------------------------------
bridge_state_dQ: deque([70])
time: 2, weight_sum: 7, bridge_state_dQ[0]: 7, truck_weights_dQ[0]: 4 
False
------------------------------------------------------------
bridge_state_dQ: deque([04])
time: 3, weight_sum: 4, bridge_state_dQ[0]: 0, truck_weights_dQ[0]: 5 
False
------------------------------------------------------------
bridge_state_dQ: deque([45])
time: 4, weight_sum: 9, bridge_state_dQ[0]: 4, truck_weights_dQ[0]: 6 
True
------------------------------------------------------------
bridge_state_dQ: deque([50])
time: 5, weight_sum: 5, bridge_state_dQ[0]: 5, truck_weights_dQ[0]: 6 
False
------------------------------------------------------------
time: 6, bridge_length: 2
answer : 8 / correct_answer : 8
테스트 0를 통과하였습니다.
----------------------------------------
<<<<<  case 1  >>>>>
truck_weights_dQ: deque([10])
bridge_state_dQ: deque([0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000])
time: 1, bridge_length: 100
answer : 101 / correct_answer : 101
테스트 1를 통과하였습니다.
----------------------------------------
<<<<<  case 2  >>>>>
truck_weights_dQ: deque([10101010101010101010])
bridge_state_dQ: deque([0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000])
bridge_state_dQ: deque([00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010])
time: 1, weight_sum: 10, bridge_state_dQ[0]: 0, truck_weights_dQ[0]: 10 
False
------------------------------------------------------------
bridge_state_dQ: deque([000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001010])
time: 2, weight_sum: 20, bridge_state_dQ[0]: 0, truck_weights_dQ[0]: 10 
False
------------------------------------------------------------
bridge_state_dQ: deque([0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101010])
time: 3, weight_sum: 30, bridge_state_dQ[0]: 0, truck_weights_dQ[0]: 10 
False
------------------------------------------------------------
bridge_state_dQ: deque([00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010101010])
time: 4, weight_sum: 40, bridge_state_dQ[0]: 0, truck_weights_dQ[0]: 10 
False
------------------------------------------------------------
bridge_state_dQ: deque([000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001010101010])
time: 5, weight_sum: 50, bridge_state_dQ[0]: 0, truck_weights_dQ[0]: 10 
False
------------------------------------------------------------
bridge_state_dQ: deque([0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101010101010])
time: 6, weight_sum: 60, bridge_state_dQ[0]: 0, truck_weights_dQ[0]: 10 
False
------------------------------------------------------------
bridge_state_dQ: deque([00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010101010101010])
time: 7, weight_sum: 70, bridge_state_dQ[0]: 0, truck_weights_dQ[0]: 10 
False
------------------------------------------------------------
bridge_state_dQ: deque([000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001010101010101010])
time: 8, weight_sum: 80, bridge_state_dQ[0]: 0, truck_weights_dQ[0]: 10 
False
------------------------------------------------------------
bridge_state_dQ: deque([0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101010101010101010])
time: 9, weight_sum: 90, bridge_state_dQ[0]: 0, truck_weights_dQ[0]: 10 
False
------------------------------------------------------------
time: 10, bridge_length: 100
answer : 110 / correct_answer : 110
테스트 2를 통과하였습니다.
----------------------------------------
 
Process finished with exit code 0
 
cs