Skip to content

Commit c8d9e51

Browse files
authored
Merge pull request #839 from AlgorithmWithGod/suyeun84
[20250907] PGM / LV2 / 다리를 지나는 트럭 / 김수연
2 parents 46aaf50 + f9c0bdc commit c8d9e51

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
```java
2+
import java.util.*;
3+
4+
class Solution {
5+
public int solution(int bridge_length, int weight, int[] truck_weights) {
6+
Queue q = new LinkedList();
7+
8+
for(int i=0;i<bridge_length-1;i++){
9+
q.add(0);
10+
}
11+
12+
int current_w = truck_weights[0];
13+
q.add(current_w);
14+
15+
int answer = 1;
16+
int index = 1;
17+
18+
while(!q.isEmpty()){
19+
answer++;
20+
21+
int removed = (int) q.poll();
22+
current_w -= removed;
23+
24+
if(index < truck_weights.length){
25+
if(current_w + truck_weights[index] <= weight){
26+
current_w += truck_weights[index];
27+
q.add(truck_weights[index]);
28+
index++;
29+
}else{
30+
q.add(0);
31+
}
32+
}
33+
}
34+
return answer;
35+
}
36+
}
37+
```

0 commit comments

Comments
 (0)