-
Notifications
You must be signed in to change notification settings - Fork 0
/
42583.java
35 lines (28 loc) · 833 Bytes
/
42583.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.*;
class Solution {
public int solution(int bridge_length, int weight, int[] truck_weights) {
int time = 0;
int truckIndex = 0;
int curWeight = 0;
LinkedList<Integer> queue = new LinkedList<>();
for(int i=0; i<bridge_length; i++) {
queue.add(0);
}
while(true) {
time++;
curWeight-=queue.poll();
if(curWeight + truck_weights[truckIndex] <= weight) {
curWeight += truck_weights[truckIndex];
queue.add(truck_weights[truckIndex++]);
if(truckIndex == truck_weights.length) {
time+=bridge_length;
break;
}
}
else {
queue.add(0);
}
}
return time;
}
}