-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay3.java
More file actions
35 lines (31 loc) · 966 Bytes
/
Day3.java
File metadata and controls
35 lines (31 loc) · 966 Bytes
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
package Algorithm;
import java.util.Queue;
import java.util.ArrayDeque;
class Day3 {
public static void main(String[] args) {
int[] array = new int[] {10};
System.out.println(solution(100, 100, array));
}
public static int solution(int bridge_length, int weight, int[] truck_weights) {
Queue<Integer> bridge = new ArrayDeque<>();
int answer = 0;
for(int i=0; i<bridge_length; i++){
bridge.add(0);
}
int truckIdx = 0;
int trucksWeight = 0;
while(truckIdx < truck_weights.length){
answer++;
trucksWeight -= bridge.poll();
if(trucksWeight + truck_weights[truckIdx] <= weight){
bridge.add(truck_weights[truckIdx]);
trucksWeight += truck_weights[truckIdx];
truckIdx++;
}
else {
bridge.add(0);
}
}
return bridge_length + answer;
}
}