슈콩

[프로그래머스] Lv.2 다리를 지나는 트럭 본문

Algorithms/Programmers

[프로그래머스] Lv.2 다리를 지나는 트럭

shukong 2025. 10. 17. 16:10

 

 

[문제]

https://school.programmers.co.kr/learn/courses/30/lessons/42583

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

 

[소스 코드]

import java.util.*;
class Solution {
    public int solution(int bridge_length, int weight, int[] truck_weights) {
        Queue<Integer> q = new LinkedList<>();
        int time = 0;
        int idx = 0;
        int total = 0;
        int n = truck_weights.length;
        for(int i=0;i<bridge_length;i++) q.offer(0);
        while(idx<n){
            total -= q.poll();
            if(truck_weights[idx]+total <= weight){
                total += truck_weights[idx];
                q.offer(truck_weights[idx++]);
            }
            else q.offer(0);
            time++;
        }
        time += bridge_length;
        return time;
    }
}