[プログラマー/queue/level 2]機能の開発


[プログラマ]機能の開発

1.問題の説明

  • 日ごとに追加を行い、最前線の作業が完了しているかを確認します.
    前の
  • が完了したら、완료한 작업은 큐에서 제거다음 작업의 완료 여부を確認してください.
  • 前の作業が完了していない場合は、
  • コマンドを発行します.
  • 作業があれば、일자(day)를 증가.
  • 2.トラブルシューティングコード

    import java.util.*;
    
    class Solution {
        public int[] solution(int[] progresses, int[] speeds) {
            ArrayList<Integer> answerList = new ArrayList<>();
            Queue<Work> queue = new LinkedList<>();
    
            for (int i = 0; i < progresses.length; i++) {
                queue.offer(new Work(progresses[i], speeds[i]));
            }
    
            int days = 1;
    
            while (!queue.isEmpty()) {
                int cnt = 0;
                while (!queue.isEmpty()) {
                    Work work = queue.peek();
                    if (!work.isDone(days)) {
                        break;
                    }
                    queue.poll();
                    cnt++;
                }
    
                if (cnt > 0) {
                    answerList.add(cnt);
                }
                days++;
            }
    
            return answerList.stream().mapToInt(i -> i).toArray();
        }
    }
    
    class Work {
        private int progress;
        private int speed;
    
        Work(int progress, int speed) {
            this.progress = progress;
            this.speed = speed;
        }
    
        public boolean isDone(int days) {
            return this.progress + (days * speed) >= 100;
        }
    }