Skip to content

Commit eb456b3

Browse files
committed
[PGS] 다리를 지나는 트럭 (Java) / Level2 (Java 풀이) / 52분 / 실패
1 parent 7a12ae7 commit eb456b3

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int solution(int bridge_length, int weight, int[] truck_weights) {
5+
6+
Queue<Integer> bridge = new LinkedList<>();
7+
for (int i = 0; i < bridge_length; i++) {
8+
bridge.offer(0); // 처음에 빈 칸으로 채워둔다.
9+
}
10+
11+
int time = 0; // 전체 경과 시간
12+
int current = 0; // 다리 위 현재 무게 합
13+
int idx = 0; // 다음에 보낼 트럭 인덱스
14+
15+
while (idx < truck_weights.length || current > 0) {
16+
time++;
17+
// 1. 다리에서 한 칸 빠져나옴
18+
int left = bridge.poll();
19+
current -= left; // 빠져나간 게 0일수도 있고, 트럭일 수도 있음.
20+
21+
// 2. 다음 트럭을 올릴 수 있으면 올리기, 아니면 빈 칸(0) 올리기
22+
if (idx < truck_weights.length && current + truck_weights[idx] <= weight) {
23+
bridge.offer(truck_weights[idx]);
24+
current += truck_weights[idx];
25+
idx++;
26+
}
27+
else {
28+
bridge.offer(0);
29+
}
30+
}
31+
return time;
32+
}
33+
}

0 commit comments

Comments
 (0)