Cute Dog Bopping Head
본문 바로가기
Code IT/Algorithm

[프로그래머스] 기능개발 - Stack,Queue (Java)

by 찾 2021. 8. 16.
import java.util.*;

class Solution {
    public int[] solution(int[] progresses, int[] speeds) {
        Queue<Integer> queue = new LinkedList<>();
        
        //각 작업 당 필요한 날의 수를 큐에 저장.
        for(int i=0;i<progresses.length;i++){
            int remainProgress = 100 - progresses[i];
            int needDay = remainProgress / speeds[i];
            if(remainProgress % speeds[i] > 0) //나머지 존재 시 1일 추가.
                needDay += 1;
            queue.offer(needDay);//스택은 push & pop, 큐는 offer & poll
        }
        
        //현재 poll되는 날보다 더 긴 기간이 필요한 작업이 나올때까지 poll.
        int dayPassed = queue.poll();
        ArrayList<Integer> result = new ArrayList<>();
        int arrayListIndex = 0;
        result.add(1);
        while(!queue.isEmpty()){
            int dayNeeded = queue.peek();
            if(dayPassed >= dayNeeded){
                queue.poll();
                int plusToCurrentIndex = result.get(arrayListIndex) + 1;
                result.remove(arrayListIndex);
                result.add(plusToCurrentIndex);
            }else{
                dayPassed = dayNeeded;
                queue.poll();
                result.add(1);
                arrayListIndex++;
            }
        }
        
        int[] answer = new int[result.size()];
        for(int i=0;i<result.size();i++)
            answer[i] = result.get(i);
        return answer; 
    }
}

스택은 push and pop, 큐는 offer and poll 

둘 다 peek 가능!

peek-a-boo :)

댓글