-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPMS_42586.java
More file actions
48 lines (41 loc) · 1.32 KB
/
PMS_42586.java
File metadata and controls
48 lines (41 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package programmers;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class PMS_42586 {
public int[] solution(int[] progresses, int[] speeds) {
List<Integer> days = calculateDays(progresses, speeds);
ArrayList<Integer> answer = new ArrayList<>();
Stack<Integer> stack = new Stack<>();
int target = 0;
for (Integer day : days) {
if (stack.isEmpty()) {
stack.push(day);
target = day;
} else if (target >= day) {
stack.push(day);
} else {
answer.add(stack.size());
target = day;
stack.clear();
stack.push(day);
}
}
if (!stack.isEmpty()) {
answer.add(stack.size());
}
return answer.stream().mapToInt(x -> x).toArray();
}
public static List<Integer> calculateDays(int[] progresses, int[] speeds) {
ArrayList<Integer> days = new ArrayList<>();
for (int i = 0; i < progresses.length; i++) {
int day = (100 - progresses[i]) / speeds[i];
if ((100 - progresses[i]) % speeds[i] == 0) {
days.add(day);
} else {
days.add(day + 1);
}
}
return days;
}
}