프로그래머스: 다리를 지나는 트럭

김아무개·2023년 4월 9일
0

다시 풀어볼 문제

목록 보기
5/8

Chat 코드

import java.util.LinkedList;
import java.util.Queue;

public class Solution {
    public int solution(int bridge_length, int weight, int[] truck_weights) {
        int time = 0;
        int currentWeight = 0;
        Queue<Integer> bridge = new LinkedList<>();

        for (int truckWeight : truck_weights) {
            while (true) {
                if (bridge.isEmpty()) {
                    bridge.offer(truckWeight);
                    currentWeight += truckWeight;
                    time++;
                    break;
                } else if (bridge.size() == bridge_length) {
                    currentWeight -= bridge.poll();
                } else {
                    if (currentWeight + truckWeight <= weight) {
                        bridge.offer(truckWeight);
                        currentWeight += truckWeight;
                        time++;
                        break;
                    } else {
                        bridge.offer(0);
                        time++;
                    }
                }
            }
        }

        return time + bridge_length;
    }
}

어휴 머리아파
이 문제는 내일의 나에게로 보내봅니다 🙈

profile
Hello velog! 

0개의 댓글