https://programmers.co.kr/learn/courses/30/lessons/42583
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def solution(bridge_length, weight, truck_weights):
answer = 0
n_trucks = len(truck_weights)
n_ends = 0
bridge = [0] * bridge_length
while n_ends < n_trucks:
answer += 1
if len(bridge) > 0:
truck = bridge.pop(0)
bridge.append(0)
if truck != 0:
n_ends += 1
if len(truck_weights) == 0:
continue
if (bridge[-1] == 0) and (sum(bridge) + truck_weights[0] <= weight):
cur_truck = truck_weights.pop(0)
bridge[-1] = cur_truck
return answer
Complexity
$O(n)$
- $n$ = len(truck_weights)
PREVIOUSEtc