[Programmers/JAVA] 기능개발

2023. 2. 16. 19:23자바/프로그래머스

내 코드

import java.util.*;
class Solution {
    public int[] solution(int[] progresses, int[] speeds) {
        int[] answer;

        //queue 생성
        Queue<Integer> queue=new LinkedList<>();

        //100%달성시까지 걸린 날짜 count해서 queue에 넣어줌
        int index=0;
        for(int prog:progresses) {
            int count=0;
            while(prog<100) {
                prog+=speeds[index];
                count++;
            }
            queue.add(count);
            index++;
        }

        //첫번째가 첫 우선순위이니 비교대상에 넣어줌
        int day=queue.peek();
        //배열인 answer대신 list를 만들어줌.
        List<Integer> day_count_list=new ArrayList<>();
        int count=0;//업데이트할 갯수
        while(queue.size()!=0) {
            if(queue.peek()<=day) {//queue의 head가 비교대상보다 작거나 같으면
                queue.remove();//삭제후
                count++;//갯수 추가
            }else {//queue의 head가 비교대상보다 크면(업데이트가 막히면)
                day_count_list.add(count);//업데이트 갯수를 list에 추가
                day=queue.peek();//비교대상 업데이트
                count=0;
            }
        }
        day_count_list.add(count);//마지막은 수행하지 않으니까 추가.

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

 

느낀점

'자바 > 프로그래머스' 카테고리의 다른 글

[Programmers/JAVA] 귤 고르기  (0) 2023.02.20
[Programmers/JAVA] n^2 배열 자르기  (0) 2023.02.17
[Programmers/JAVA] [1차] 캐시  (0) 2023.02.15
[Programmers/JAVA] 위장  (0) 2023.02.14
[Programmers/JAVA] 튜플  (0) 2023.02.13