https://school.programmers.co.kr/learn/courses/30/lessons/42583
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
유령 트럭을 안 쓰고 하다가 포기
1초마다 while문 돌리는 풀이
옛날 나의 풀이:
유령 트럭 + add 후 remove
지금 나의 풀이:
유령 트럭 + remove 후 add
import java.util.*;
class Solution {
public int solution(int bridge_length, int weight, int[] truck_weights) {
int answer = 0;
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < bridge_length; i++) queue.add(0);
int truck_index = 0;
int sum_truck_weights = 0;
while (true) {
answer++;
sum_truck_weights -= queue.remove();
if (sum_truck_weights + truck_weights[truck_index] <= weight) {
queue.add(truck_weights[truck_index]);
sum_truck_weights += truck_weights[truck_index];
truck_index++;
} else
queue.add(0);
if (truck_index == truck_weights.length) {
answer += bridge_length;
break;
}
}
return answer;
}
}