-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path506. Relative Ranks
35 lines (30 loc) · 1.04 KB
/
506. Relative Ranks
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
public class Solution {
public String[] findRelativeRanks(int[] nums) {
// PriorityQueue could but not great enough, use array
Integer[] index = new Integer[nums.length];
for (int i = 0; i < nums.length; i++) {
index[i] = i;
}
// jdk 8 syntax
// List<CustomObject> list = getCustomObjectList();
// Collections.sort(list, (left, right) -> left.getId() - right.getId());
// System.out.println(list);
Arrays.sort(index, (a, b) -> (nums[b] - nums[a]));
String[] result = new String[nums.length];
for (int i = 0; i < nums.length; i++) {
if (i == 0) {
result[index[i]] = "Gold Medal";
}
else if (i == 1) {
result[index[i]] = "Silver Medal";
}
else if (i == 2) {
result[index[i]] = "Bronze Medal";
}
else {
result[index[i]] = (i + 1) + "";
}
}
return result;
}
}