728x90
2023.07.11 - [Coding Test/programmers] - 다리를 지나는 트럭 (다리 큐, 대기 큐)
import java.util.*;
class Solution {
public int solution(int bridge_length, int weight, int[] truck_weights) {
int answer = 0; // 소요시간
// 다리 큐
Queue<Integer> bridge = new LinkedList<>();
for (int i=0; i<bridge_length; i++){
bridge.add(0);
}
// 대기 큐
Queue<Integer> waitTrucks = new LinkedList<>();
for (int i=0; i<truck_weights.length; i++){
waitTrucks.add(truck_weights[i]);
}
int bridgeWeight = 0; // 다리 무게
while(waitTrucks.size()>0){
bridgeWeight -= bridge.poll();
if (waitTrucks.size()>0 && bridgeWeight + waitTrucks.peek() <= weight){ // 견딜수 있는 무게
int truckWeight = waitTrucks.poll();
bridge.add(truckWeight);
bridgeWeight+=truckWeight;
}
else{
bridge.add(0);
}
answer++;
}
while(bridgeWeight>0){
bridgeWeight-=bridge.poll();
answer++;
}
return answer;
}
}
import java.util.*;
class Solution {
public int solution(int bridge_length, int weight, int[] truck_weights) {
int answer = 0; // 소요시간
Queue<Integer> bridge = new LinkedList<>();
for (int i=0; i<bridge_length; i++){
bridge.add(0);
}
int bridgeWeight = 0; // 다리 무게
int truckIdx = 0; // 대기중인 트럭 인덱스
while(truckIdx < truck_weights.length){
bridgeWeight -= bridge.poll();
int truckWeight = truck_weights[truckIdx];
if (bridgeWeight+truckWeight <= weight){ // 견딜수 있는 무게
bridge.add(truckWeight);
bridgeWeight+=truckWeight;
truckIdx++;
}
else{
bridge.add(0);
}
answer++;
}
while(bridgeWeight>0){
bridgeWeight-=bridge.poll();
answer++;
}
return answer;
}
}
728x90