시도 1. Stack 이용 → 통과
import java.util.*;
class Solution {
public int[] solution(int[] progresses, int[] speeds) {
Stack<Integer> stack = new Stack<>();
int[] answer = {};
for (int i = progresses.length - 1; i >= 0; i--) {
stack.push(progresses[i]);
}
int day = 1;
int idx = 0;
int outCount = 0;
while (true) {
if (stack.isEmpty()) {
answer = Arrays.copyOf(answer, answer.length + 1);
answer[answer.length - 1] = outCount;
break;
}
if ((stack.peek() + (day * speeds[idx])) >= 100) {
outCount++;
stack.pop();
idx++;
} else {
if (outCount != 0) {
answer = Arrays.copyOf(answer, answer.length + 1);
answer[answer.length - 1] = outCount;
}
outCount = 0;
day++;
}
}
return answer;
}
}
모범 답안
import java.util.ArrayList;
import java.util.Arrays;
class Solution {
public int[] solution(int[] progresses, int[] speeds) {
int[] dayOfend = new int[100];
int day = -1;
for(int i=0; i<progresses.length; i++) {
while(progresses[i] + (day*speeds[i]) < 100) {
day++;
}
dayOfend[day]++;
}
return Arrays.stream(dayOfend).filter(i -> i!=0).toArray();
}
}