Skip to content

Commit 953320f

Browse files
committed
[PGS] 42583 다리를 지나는 트럭 (Lv.2)
1 parent b099b3d commit 953320f

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

이용훈/8주차/260219.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function solution(bridge_length, weight, truck_weights) {
2+
let sum = 0;
3+
let answer = 0;
4+
5+
const bridge = Array(bridge_length).fill(0);
6+
while(truck_weights.length || sum > 0) {
7+
answer++;
8+
9+
const cur = bridge.shift();
10+
sum -= cur;
11+
12+
if(truck_weights.length) {
13+
const next = truck_weights[0]
14+
if(sum + next <= weight) {
15+
bridge.push(next);
16+
sum += next;
17+
truck_weights.shift();
18+
} else {
19+
bridge.push(0);
20+
}
21+
} else {
22+
bridge.push(0);
23+
}
24+
}
25+
26+
return answer;
27+
}

0 commit comments

Comments
 (0)