[프로그래머스] 스택/큐 - 기능개발(자바)

import java.util.ArrayList;
import java.util.Arrays;

class Solution {
    public int[] solution(int[] progresses, int[] speeds) {
        int[] leftover = new int[progresses.length];
        for(int i = 0; i <progresses.length ; i++) {
            int day = 1;
            while(progresses[i] + speeds[i]*day < 100) {
                day++;
            }
            leftover[i] = day;
        }
        System.out.println(Arrays.toString(leftover));

        ArrayList<Integer> list = new ArrayList<>();
        for(int i = 0; i<leftover.length ; i++) {
            int cnt = 1;
            if(leftover[i] < 0) continue;
            for(int j = i+1; j<leftover.length; j++) {
                if(leftover[i] >= leftover[j]) {
                    leftover[j] = -1;
                    cnt++;
                } else {
                    break;
                }
            }
            list.add(cnt);
        }
        System.out.println(list);
        int[] answer = list.stream().mapToInt(i -> i).toArray();
        return answer;
    }

    public static void main(String[] args) {
        int[] progress = {95,90,99,99,80,99};
        int[] speed = {1,1,1,1,1,1};
        Solution st = new Solution();
        st.solution(progress,speed);
    }
}
import java.util.*;

class Solution {
    public int[] solution(int[] progresses, int[] speeds) {
        Queue<Integer> q = new LinkedList<>();
        List<Integer> answerList = new ArrayList<>();

        for(int i = 0; i < speeds.length; i++) {
            int remain = (int) Math.ceil(100 - progresses[i] / speeds[i]);

            if(!q.isEmpty() && q.peek() < remain) {
                answerList.add(q.size());
                q.clear();
            }
            q.offer(remain);
        }
        answerList.add(q.size());

        int[] answer = new int[answerList.size()];

        for(int i = 0; i < answer.length; i++) {
            answer[i] = answerList.get(i);
        }
        return answer;
    }

    public static void main(String[] args) {
        int[] progress = {95,90,99,99,80,99};
        int[] speed = {1,1,1,1,1,1};
        Solution st = new Solution();
        System.out.println(Arrays.toString(st.solution(progress,speed)));
    }
}

좋은 웹페이지 즐겨찾기