현재 진도 progresses와 개발 속도 speeds가 주어질 때, 배포되는 기능의 개수를 순서대로 반환하는 문제이다.
앞의 기능이 먼저 배포되어야 뒤의 기능도 함께 배포할 수 있다.
먼저 각 기능이 완성되기까지 필요한 일수를 계산한다.
그다음 Queue에 순서대로 넣고 앞에서부터 확인한다.
속도로 나누어 완료까지 필요한 일수를 구한다.Queue에 저장한다.List를 int[] 배열로 변환한다.import java.util.*;
class Solution {
public int[] solution(int[] progresses, int[] speeds) {
Queue<Integer> daysQueue = new LinkedList<>();
// 1. 각 작업별 완료까지 걸리는 일수 계산
for (int i = 0; i < progresses.length; i++) {
int remain = 100 - progresses[i];
int day = remain / speeds[i];
if (remain % speeds[i] != 0) {
day += 1;
}
daysQueue.add(day);
}
List<Integer> answerList = new ArrayList<>();
// 2. 큐를 돌면서 함께 배포될 수 있는 개수 계산
int maxDay = daysQueue.poll();
int count = 1;
while (!daysQueue.isEmpty()) {
int nextDay = daysQueue.poll();
if (maxDay >= nextDay) {
count++;
} else {
answerList.add(count);
count = 1;
maxDay = nextDay;
}
}
// 마지막 배포 그룹 추가
answerList.add(count);
// 3. List를 배열로 변환
return answerList.stream()
.mapToInt(i -> i)
.toArray();
}
}
