Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9ae2a92

Browse files
Update 다리를 지나는 트럭.js
1 parent 0a26511 commit 9ae2a92

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

‎level-2/다리를-지나는-트럭.js‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,29 @@ function solution(bridge_length, weight, truck_weights) {
2727
bridge.unshift(truck_weights.shift()) : bridge.unshift(0)
2828
} while (bridge.reduce((a, b) => a + b) !== 0)
2929
return tick;
30-
}
30+
}
31+
32+
//정답 3 - jaewon1676
33+
function solution(bridge_length, weight, truck_weights) {
34+
var answer = 0; // 총 걸리는 시간
35+
let bridge = []; // 다리를 건너는 트럭
36+
let bridge_weight = 0; // 다리를 건너는 트럭의 총 무게
37+
38+
while (truck_weights.length > 0) { // 대기 트럭이 없을때까지 반복한다.
39+
answer++; // 1초 추가
40+
if (bridge.length == bridge_length) { // 다리가 가득 차있으면 제일 먼저 들어간 트럭을 뺴준다.
41+
bridge_weight -= bridge.shift();
42+
}
43+
if (bridge_weight + truck_weights[0] > weight) {
44+
bridge.push(0);
45+
continue;
46+
}
47+
let truck_weight = truck_weights.shift();
48+
bridge.push(truck_weight);
49+
bridge_weight += truck_weight;
50+
}
51+
52+
answer += bridge_length;
53+
54+
return answer;
55+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /