import java.util.LinkedList;
import java.util.Queue;
public class Truck {
public int solution(int bridge_length, int weight, int[] truck_weights) {
int answer = 1;
Queue<Integer> bridgeQ = new LinkedList<>();
Queue<Integer> truckQ = new LinkedList<>();
for (int ele : truck_weights) {
truckQ.offer(ele);
}
for (int i = 0; i < bridge_length - 1; i++) {
bridgeQ.offer(0);
}
int current_weight = 0;
while (!bridgeQ.isEmpty()) {
if (!truckQ.isEmpty()) {
if (current_weight + truckQ.peek() <= weight) {
current_weight += truckQ.peek();
bridgeQ.offer(truckQ.poll());
} else {
bridgeQ.offer(0);
}
}
current_weight -= bridgeQ.poll();
answer++;
}
return answer;
}
public static void main(String[] args) {
Truck s = new Truck();
int[] truck_weights1 = { 7, 4, 5, 6 };
int[] truck_weights2 = { 10 };
int[] truck_weights3 = { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 };
System.out.println(s.solution(2, 10, truck_weights1));
System.out.println(s.solution(100, 100, truck_weights2));
System.out.println(s.solution(100, 100, truck_weights3));
}
}