- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution506.java
More file actions
Latest commit
executable file
·37 lines (33 loc) · 1.12 KB
/
Copy pathSolution506.java
File metadata and controls
executable file
·37 lines (33 loc) · 1.12 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
importsun.font.GlyphLayout;
importjava.util.Arrays;
importjava.util.HashMap;
publicclassSolution506 {
publicString[] findRelativeRanks(int[] nums) {
int[] tmp = newint[nums.length];
System.arraycopy(nums,0,tmp,0,nums.length);
String[] ans = newString[nums.length];
Arrays.sort(tmp);
intlen;
len = nums.length;
HashMap<Integer,Integer> hashmap = newHashMap<>();
for (inti = 0; i < len; i++) {
hashmap.put(tmp[i],i+1);
}
inti = 0;
for (intnum : nums) {
if (hashmap.get(num)==len) ans[i] = "Gold Medal";
if (hashmap.get(num)==len-1) ans[i] = "Silver Medal";
if (hashmap.get(num)==len-2) ans[i] = "Bronze Medal";
if (hashmap.get(num)<len-2){
ans[i] = Integer.toString(len+1-hashmap.get(num));
}
i+=1;
}
returnans;
}
publicstaticvoidmain(String[] args) {
Solution506s = newSolution506();
int[] nums = {5, 4, 3, 2, 1};
System.out.println(Arrays.toString(s.findRelativeRanks(nums)));
}
}