PROG 다리를 지나는 트럭
-
변수
LinkedList<Integer> bridge; //다리를 나타내는 Queue int curTime; //현재 걸린 시간 int curWeight; //현재 다리의 무게 class Truck{ int weight; int time; }
-
로직
-
다리에 무게가 가능하다면 트럭을 한개씩 이동
-
다리를 빠져나갈 트럭이 있는지 확인
- 있다면 뺀다.
-
시간을 증가시킨다.
-
-
코드
import java.util.*;
class Solution {
public int solution(int bridge_length, int weight, int[] truck_weights) {
int curTime = 1;
int curWeight = 0;
int truckIdx = 0;
LinkedList<Truck> bridge = new LinkedList<Truck>();
while(true){
if(!bridge.isEmpty() && bridge.peek().time == curTime - bridge_length){
curWeight -= bridge.poll().weight;
if(truckIdx == truck_weights.length && bridge.isEmpty())
break;
}
if(truckIdx < truck_weights.length && curWeight + truck_weights[truckIdx] <= weight){
bridge.offer(new Truck(truck_weights[truckIdx], curTime));
curWeight += truck_weights[truckIdx];
truckIdx++;
}
curTime++;
}
return curTime;
}
static class Truck{
int weight;
int time;
public Truck(int weight, int time){
this.weight = weight;
this.time = time;
}
}
}
Leave a comment