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 :)
'Code IT > Algorithm' 카테고리의 다른 글
[프로그래머스] 다리를 지나는 트럭 - Stack, Queue (Java) (0) | 2021.08.21 |
---|---|
[프로그래머스] 프린터 - Stack,Queue (Java) (0) | 2021.08.16 |
[프로그래머스] 베스트앨범 - Hash (Java) (0) | 2021.08.14 |
[백준] 17952 과제는 끝나지 않아! - Stack (Java) (0) | 2021.04.06 |
[백준] 9251 LCS - Dynamic Programming (Java) (0) | 2021.04.03 |
댓글