- 난이도: Lv2
프로그래머스 링크: https://school.programmers.co.kr/learn/courses/30/lessons/42583
풀이 링크(GitHub): hayannn/CodingTest_Java/프로그래머스/2/다리를 지나는 트럭
풀이 시간 : 18분
import java.util.*;
class Solution {
public int solution(int bridge_length, int weight, int[] truck_weights) {
Queue<Integer> bridge = new LinkedList<>();
int totalWeight = 0, time = 0, index = 0;
for (int i = 0; i < bridge_length; i++) {
bridge.offer(0);
}
while (index < truck_weights.length) {
time++;
totalWeight -= bridge.poll();
if (totalWeight + truck_weights[index] > weight) {
bridge.offer(0);
continue;
}
bridge.offer(truck_weights[index]);
totalWeight += truck_weights[index];
index++;
}
return time + bridge_length;
}
}