이전코트
import java.util.*;
class Solution {
public int solution(int bridge_length, int weight, int[] truck_weights) {
int answer = 0;
// int queueLocation = bridge_length;
int weightSum = 0;
Queue<Integer> queue = new LinkedList<>();
for (int i = 0 ; i < bridge_length ; i++){
queue.offer(0);
}
int i = 0;
while(true){
if(i < truck_weights.length){
if((weightSum + truck_weights[i] - queue.peek()) <= weight){
weightSum += truck_weights[i];
queue.offer(truck_weights[i]);
int val = queue.poll();
if ( val > 0){
weightSum -= val;
}
i ++;
} else {
queue.offer(0);
int val = queue.poll();
if ( val > 0){
weightSum -= val;
}
}
} else{
queue.poll();
}
answer ++;
if(queue.isEmpty()){
break;
}
}
return answer;
}
}
최근 코드
import java.util.*;
class Solution {
public int solution(int bridge_length, int weight, int[] truck_weights) {
int answer = 0;
Queue <Integer> bridgeQueue = new LinkedList<>();
for (int i = 0 ; i < bridge_length ; i ++){
bridgeQueue.add(0);
}
int weightSum = 0;
int time = 0;
int arriveCount = 0;
for (int i = 0 ; i < truck_weights.length ; ){
time ++;
if (bridgeQueue.peek() != 0){
weightSum -= bridgeQueue.poll();
arriveCount ++;
if (weight >= weightSum + truck_weights[i]){
weightSum += truck_weights[i];
bridgeQueue.add(truck_weights[i]);
i ++;
} else {
bridgeQueue.add(0);
}
} else {
bridgeQueue.poll();
if (weight >= weightSum + truck_weights[i]){
weightSum += truck_weights[i];
bridgeQueue.add(truck_weights[i]);
i ++;
} else {
bridgeQueue.add(0);
}
}
}
time += bridge_length;
return time;
}
}
'프로그래머스 - JAVA' 카테고리의 다른 글
코딩테스트 연습 > 힙(Heap) > 더 맵게 (0) | 2021.06.12 |
---|---|
코딩테스트 연습 > 스택/큐 > 주식가격 (0) | 2021.06.12 |
코딩테스트 연습 > 스택/큐 > 프린터 (0) | 2021.06.12 |
코딩테스트 연습 > 스택/큐 > 기능개발 (0) | 2021.06.12 |
코딩테스트 연습 > 해시 > 베스트앨범 (0) | 2021.06.12 |