Link
Code
import java.util.*;
class Solution {
public int solution(int bridge_length, int weight, int[] truck_weights) {
int answer = 0;
HashMap<Integer, Integer> map = new HashMap<>();
Queue<Integer> queue = new LinkedList<>();
Queue<Integer> queue2 = new LinkedList<>();
for (int i = 0; i < truck_weights.length; i++) {
map.put(i,0);
queue.add(truck_weights[i]);
queue2.add(truck_weights[i]);
}
int current = 0 ;
int prev = 0 ;
while(!queue2.isEmpty()){
answer ++ ;
int Q = 0 ;
int W = 0 ;
if(queue.isEmpty()){
}
else if(weight- queue.peek() >= 0 ){
if(W!=1) {
current++;
weight = weight - queue.poll();
Q++;
}else{
W=0;
}
}
for(int i =prev ; i <current; i++){
if(map.get(i) < bridge_length){
map.put(i, map.get(i)+1);
}
else if(map.get(i) == bridge_length){
map.put(i, map.get(i)+1);
weight = weight + queue2.poll();
prev ++;
if(queue.isEmpty()){
}
else if(weight- queue.peek() >= 0 ){
if(Q!=1) {
current++;
weight = weight - queue.poll();
W++;
}{
Q=0;
}
}
}
}
}
return answer;
}
}
