forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3.java
62 lines (51 loc) Β· 1.62 KB
/
3.java
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
58
59
60
61
62
import java.util.*;
class Node implements Comparable<Node> {
private int stage;
private double fail;
public Node(int stage, double fail) {
this.stage = stage;
this.fail = fail;
}
public int getStage() {
return this.stage;
}
@Override
public int compareTo(Node other) {
if (this.fail == other.fail) {
return Integer.compare(this.stage, other.stage);
}
return Double.compare(other.fail, this.fail);
}
}
class Solution {
public int[] solution(int N, int[] stages) {
int[] answer = new int[N];
ArrayList<Node> arrayList = new ArrayList<>();
int length = stages.length;
// μ€ν
μ΄μ§ λ²νΈλ₯Ό 1λΆν° NκΉμ§ μ¦κ°μν€λ©°
for (int i = 1; i <= N; i++) {
// ν΄λΉ μ€ν
μ΄μ§μ λ¨Έλ¬Όλ¬ μλ μ¬λμ μ κ³μ°
int cnt = 0;
for (int j = 0; j < stages.length; j++) {
if (stages[j] == i) {
cnt += 1;
}
}
// μ€ν¨μ¨ κ³μ°
double fail = 0;
if (length >= 1) {
fail = (double) cnt / length;
}
// 리μ€νΈμ (μ€ν
μ΄μ§ λ²νΈ, μ€ν¨μ¨) μμ μ½μ
arrayList.add(new Node(i, fail));
length -= cnt;
}
// μ€ν¨μ¨μ κΈ°μ€μΌλ‘ κ° μ€ν
μ΄μ§λ₯Ό λ΄λ¦Όμ°¨μ μ λ ¬
Collections.sort(arrayList);
// μ λ ¬λ μ€ν
μ΄μ§ λ²νΈ λ°ν
for (int i = 0; i < N; i++) {
answer[i] = arrayList.get(i).getStage();
}
return answer;
}
}