Untitled

Untitled

시도 1. 종료조건을 truck_weights 배열의 순회로 생각

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

class Solution {
    public int solution(int bridge_length, int weight, int[] truck_weights) {
         int time = 1;
                int currentWeight = 0;
                List<Integer> arriveTrucks = new ArrayList<>();
                Queue<Integer> queue = new LinkedList<>();
                for (int i = 0; i < bridge_length; i++) {
                    queue.add(0);
                }
                int idx = 0;
                while (true) {
                    int end = queue.poll();
                    if (end != 0) {
                        currentWeight -= end;
                        arriveTrucks.add(end);
                    }
                    if (idx == truck_weights.length) {
                        break;
                    }
                    if (currentWeight + truck_weights[idx] <= weight) {
                        queue.add(truck_weights[idx]);
                        currentWeight += truck_weights[idx];
                        idx++;
                    } else {
                        queue.add(0);
                    }
                    time++;
                }
                return time;
    }
}

→ 배열 순회가 끝나고 나서도 다리에 있는 트럭에 대한 처리가 없음

시도 2. 배열의 순회가 끝나고 나서 다리에 있는 트럭이 모두 도착할 때 까지 queue.poll()

class Solution {
            public int solution(int bridge_length, int weight, int[] truck_weights) {
                int time = 1;
                int currentWeight = 0;
                List<Integer> arriveTrucks = new ArrayList<>();
                Queue<Integer> queue = new LinkedList<>();
                for (int i = 0; i < bridge_length; i++) {
                    queue.add(0);
                }
                int idx = 0;
                while (true) {
                    int end = queue.poll();
                    if (end != 0) {
                        currentWeight -= end;
                        arriveTrucks.add(end);
                    }
                    if (idx == truck_weights.length) {
                        break;
                    }
                    if (currentWeight + truck_weights[idx] <= weight) {
                        queue.add(truck_weights[idx]);
                        currentWeight += truck_weights[idx];
                        idx++;
                    } else {
                        queue.add(0);
                    }
                    time++;
                }
                while (!queue.isEmpty()) {
                    time++;
                    System.out.println("size = " + arriveTrucks.size());
                    int end = queue.poll();
                    if (end != 0) {
                        arriveTrucks.add(end);
                    }
                    if (arriveTrucks.size() == truck_weights.length) {
                        break;
                    }
                }
                return time;
            }
        }