Java

프로그래머스: 기능개발

Daeryuk Kim 2024. 3. 7. 15:42
import java.util.*;

class Solution {
    public int[] solution(int[] progresses, int[] speeds) {

        ArrayList<Integer> list = new ArrayList<>();
        Queue<Integer> q = new LinkedList<>();

        for (int i = 0; i < progresses.length; i++) {
            if ((100 - progresses[i]) % speeds[i] == 0) {
                q.add((100 - progresses[i]) / speeds[i]);
            } else {
                q.add((100 - progresses[i]) / speeds[i] + 1);
            }
        }

        int x = q.poll();
        int count = 1;
        while (!q.isEmpty()) {
            if (x >= q.peek()) {
                //System.out.println("1. x="+x+" count="+count);
                count++;
                q.poll();
            } else {
                //System.out.println("2. x="+x+" count="+count);
                list.add(count);
                count = 1;
                x = q.poll();
            }
        }
        list.add(count);

        int[] answer = new int[list.size()];
        for (int i = 0; i < answer.length; i++) {
            answer[i] = list.get(i);
        }


        return answer;

    }
    public static void main(String[] args) {
        int[] progresses = {93, 30, 55};
        int[] speeds = {1, 30, 5};
        Solution sol = new Solution();
        System.out.println(Arrays.toString(sol.solution(progresses, speeds)));
        
    }
}

만약, 아래의 조건이면 

입력값 〉 [95, 90, 99, 99, 80, 99], [1, 1, 1, 1, 1, 1]
기댓값 〉 [1, 3, 2]
   
출력 〉 2. x=5 count=1
1. x=10 count=1
1. x=10 count=2
2. x=10 count=3
1. x=20 count=1

5일째에 95가 끝나고

10일째에 90,99,99가 끝나고

20일째 99가 80,99가 끝나면서 

[1,3,2]를 반환하고 끝이 난다.