문제설명
생각하기
- 먼저 들어간 트럭이 먼저 나오기 때문에 Queue를 사용
내 풀이
import java.util.LinkedList;
import java.util.Queue;
class Solution {
public int solution(int bridge_length, int weight, int[] truck_weights) {
int time = 0;
int now_weight = 0;
int idx =0;
Queue<Integer> Q = new LinkedList<>();
while(true){
if(idx == truck_weights.length) break;
if( Q.size() == bridge_length) now_weight -= Q.poll();
else if( now_weight + truck_weights[idx] > weight){
Q.offer(0);
time++;
}else{
Q.offer(truck_weights[idx]);
now_weight += truck_weights[idx];
time++;
idx++;
}
}
return time + bridge_length;
}
}