-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
57 lines (46 loc) · 1.41 KB
/
Solution.java
File metadata and controls
57 lines (46 loc) · 1.41 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
49
50
51
52
53
54
55
56
57
import java.util.Arrays;
class Solution {
public int[] solution(int n, int[] users) {
int[] answer = new int[n];
Stage[] stages = new Stage[n];
for (int i = 0; i < n; i++) {
stages[i] = new Stage(i + 1);
}
for (int userStage : users) {
if (userStage <= n) {
stages[userStage - 1].count++;
}
}
int userCount = users.length;
for (Stage stage : stages) {
if (stage.count == 0 || userCount == 0) {
stage.failureRate = 0.0;
} else {
stage.failureRate = (double) stage.count / userCount;
userCount -= stage.count;
}
}
Arrays.sort(stages);
for (int i = 0; i < n; i++) {
answer[i] = stages[i].stageNumber;
}
return answer;
}
class Stage implements Comparable {
int stageNumber;
int count;
double failureRate;
public Stage(int stage) {
this.stageNumber = stage;
}
@Override
public int compareTo(Object o) {
Stage otherStage = (Stage) o;
if (this.failureRate == otherStage.failureRate) {
return Integer.compare(this.stageNumber, otherStage.stageNumber);
}
return -Double.compare(this.failureRate, otherStage.failureRate);
}
s
}
}