-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRelative-Ranks.java
47 lines (42 loc) · 1.21 KB
/
Relative-Ranks.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
class Solution {
public String[] findRelativeRanks(int[] score) {
int maxVal = maxElement(score);
int[] arr = new int[maxVal + 1];
int N = score.length;
for(int i = 0; i < N; i++){
arr[score[i]] = i + 1;
}
String[] res = new String[N];
int rank = 1;
for(int i = maxVal; i >= 0; i--){
if(arr[i] != 0){
int index = arr[i] - 1;
if(rank == 1){
res[index] = "Gold Medal";
}
else if(rank == 2){
res[index] = "Silver Medal";
}
else if(rank == 3){
res[index] = "Bronze Medal";
}
else{
res[index] = Integer.toString(rank);
}
rank++;
}
// Smaller Optimization
if(rank > N){
break;
}
}
return res;
}
public int maxElement(int[] score){
int max = Integer.MIN_VALUE;
for(int e : score){
max = Math.max(max, e);
}
return max;
}
}