-
Notifications
You must be signed in to change notification settings - Fork 1
/
RelativeRanks.java
76 lines (65 loc) · 1.64 KB
/
RelativeRanks.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package leetcode2018;
import java.util.HashMap;
import java.util.Map;
public class RelativeRanks {
public String[] findRelativeRanks(int[] nums) {
Map<Integer,String> map = new HashMap<>();
String[] str = new String[nums.length];
int n = nums.length-1;
int[] copy = new int[n+1];
for(int i=0; i<= n; i++){
copy[i]= nums[i];
}
mergeSort(nums, 0, nums.length-1);
for(int i=0; i< nums.length; i++){
if(i==0){
map.put(nums[n-i], "Gold Medal");
}
else if(i==1){
map.put(nums[n-i], "Silver Medal");
}
else if(i==2){
map.put(nums[n-i], "Bronze Medal");
}
else{
map.put(nums[n-i], (i+1)+"");
}
}
for(int i=0; i< copy.length; i++){
str[i] = map.get(copy[i]);
}
return str;
}
public void mergeSort(int[] nums, int l, int p){
if(l==p){
return;
}
int mid = (l+p)/2;
mergeSort(nums, l, mid);
mergeSort(nums, mid+1, p);
merge(nums, l, mid+1, p);
}
public void merge(int[] nums, int left, int mid, int rightEnd){
int right = mid;
int k=0;
int n= rightEnd-left+1;
int lower =left;
int[] temp= new int[n];
while(left<=mid-1 && right<= rightEnd){
if(nums[left]<=nums[right]){
temp[k++]= nums[left++];
}else{
temp[k++]= nums[right++];
}
}
while(left<=mid-1){
temp[k++]= nums[left++];
}
while(right<=rightEnd){
temp[k++]= nums[right++];
}
for(int i=0; i<n;i++){
nums[lower+i] = temp[i];
}
}
}