Coding Test/JAVA 코딩테스트 풀이정리(프로그래머스)

프로그래머스 스쿨(Java - Lv.2) - 기능개발[스택/큐 ? 길이가 정해지지 않은 배열]
깝몬 2025. 1. 28. 00:08

문제

https://school.programmers.co.kr/learn/courses/30/lessons/42586

 

 

풀이

import java.util.*;

class Solution {
    public int[] solution(int[] progresses, int[] speeds) {
        List<Integer> list = new ArrayList<>();
        int toUpdate=0;
        while(toUpdate<progresses.length){
            for(int i=0;i<progresses.length;i++){
                progresses[i]+=speeds[i];
            }
            if(progresses[toUpdate]>=100){
                int updateCnt = 1;
                toUpdate++;
                while(toUpdate<progresses.length && progresses[toUpdate]>=100){
                    toUpdate++;
                    updateCnt++;
                }
                list.add(updateCnt);
            }
        }
        return list.stream().mapToInt(i -> i).toArray();
    }
}

 

 

해설

 

문제의 의도는 스택이나 큐를 사용하여 스택에 내용물을 저장 후 그에 대한 처리를 요구한 것 같으나...

 

길이가 정해지지않은 ArrayList를 사용하여 처리 후 stream을 이용하여 배열로 돌릴 경우 문제 없이 해결되었다.