[프로그래머스] 기능개발 (JAVA)

dev_kiiim·2022년 12월 13일
0

CODING TEST

목록 보기
17/23
post-thumbnail

오늘 정리할 문제는 어제부터 풀다가 오늘 완료한 프로그래머스의 기능개발 문제이다.
알고리즘 분류는 스택/큐 문제이고 문제 자체를 이해하기에는 어려움은 없었다.

작업의 순서가 중요하기 때문에 큐를 이용하여 접근하였다.


public static ArrayList<Integer> solution(int[] progresses, int[] speeds) {
    ArrayList<Integer> answer = new ArrayList<>();
    Queue<Integer> queue = new LinkedList<>();
    int index = 0;

    for (int i = 0; i < progresses.length; i++) {
        queue.offer(progresses[i]);
    }

    while(!queue.isEmpty()){
        int cnt = 0;
        for (int i = index; i < speeds.length; i++) {
            queue.offer(queue.poll() + speeds[i]);
        }
        while (!queue.isEmpty() && queue.peek() >= 100) {
                if(queue.peek() >= 100){
                    cnt++;
                    index++;
                    queue.poll();
                }
        }
        if(cnt != 0){
            answer.add(cnt);
        }
    }
    return answer;
}

일단 입력된 progresses를 큐에 넣어주고,
배열의 길이만큼 반복해서 모든 progresses에 해당 인덱스의 speeds를 더할 수 있도록 해주었다.
다음으로 첫번째 progresses가 100이 되었을 때 배포가 가능하므로 해당 부분을 구현해주었는데, 그 다음 progresses도 100이라면 같이 배포되어야 하고 배포되는 progresses의 갯수를 cnt로 카운트하여 출력할 수 있도록 구현하였다.

0개의 댓글