@@ -41,17 +41,11 @@ class Solution:
41
41
n = len (score)
42
42
idx = list (range (n))
43
43
idx.sort(key = lambda x : - score[x])
44
- res = [None ] * n
44
+ top3 = [' Gold Medal' , ' Silver Medal' , ' Bronze Medal' ]
45
+ ans = [None ] * n
45
46
for i in range (n):
46
- if i == 0 :
47
- res[idx[i]] = ' Gold Medal'
48
- elif i == 1 :
49
- res[idx[i]] = ' Silver Medal'
50
- elif i == 2 :
51
- res[idx[i]] = ' Bronze Medal'
52
- else :
53
- res[idx[i]] = str (i + 1 )
54
- return res
47
+ ans[idx[i]] = top3[i] if i < 3 else str (i + 1 )
48
+ return ans
55
49
```
56
50
57
51
### ** Java**
@@ -60,30 +54,70 @@ class Solution:
60
54
61
55
``` java
62
56
class Solution {
63
- public String [] findRelativeRanks (int [] nums ) {
64
- int n = nums . length;
65
- Integer [] index = new Integer [n];
57
+ public String [] findRelativeRanks (int [] score ) {
58
+ int n = score . length;
59
+ Integer [] idx = new Integer [n];
66
60
for (int i = 0 ; i < n; ++ i) {
67
- index [i] = i;
61
+ idx [i] = i;
68
62
}
69
- Arrays . sort(index, (o1, o2) - > Integer . compare(nums[o2], nums[o1]));
70
- String [] res = new String [n];
63
+ Arrays . sort(idx, (i1, i2) - > score[i2] - score[i1]);
64
+ String [] ans = new String [n];
65
+ String [] top3 = new String []{" Gold Medal" , " Silver Medal" , " Bronze Medal" };
71
66
for (int i = 0 ; i < n; ++ i) {
72
- if (i == 0 ) {
73
- res[index[i]] = " Gold Medal" ;
74
- } else if (i == 1 ) {
75
- res[index[i]] = " Silver Medal" ;
76
- } else if (i == 2 ) {
77
- res[index[i]] = " Bronze Medal" ;
78
- } else {
79
- res[index[i]] = String . valueOf(i + 1 );
80
- }
67
+ ans[idx[i]] = i < 3 ? top3[i] : String . valueOf(i + 1 );
81
68
}
82
- return res ;
69
+ return ans ;
83
70
}
84
71
}
85
72
```
86
73
74
+ ### ** C++**
75
+
76
+ ``` cpp
77
+ class Solution {
78
+ public:
79
+ vector<string > findRelativeRanks(vector<int > &score) {
80
+ int n = score.size();
81
+ vector<pair<int, int>> idx;
82
+ for (int i = 0; i < n; ++i)
83
+ idx.push_back(make_pair(score[ i] , i));
84
+ sort(idx.begin(), idx.end(),
85
+ [ &] (const pair<int, int> &x, const pair<int, int> &y)
86
+ { return x.first > y.first; });
87
+ vector<string > ans(n);
88
+ vector<string > top3 = {"Gold Medal", "Silver Medal", "Bronze Medal"};
89
+ for (int i = 0; i < n; ++i)
90
+ ans[ idx[ i] .second] = i < 3 ? top3[ i] : to_string(i + 1);
91
+ return ans;
92
+ }
93
+ };
94
+ ```
95
+
96
+ ### **Go**
97
+
98
+ ```go
99
+ func findRelativeRanks(score []int) []string {
100
+ n := len(score)
101
+ idx := make([][]int, n)
102
+ for i := 0; i < n; i++ {
103
+ idx[i] = []int{score[i], i}
104
+ }
105
+ sort.Slice(idx, func(i1, i2 int) bool {
106
+ return idx[i1][0] > idx[i2][0]
107
+ })
108
+ ans := make([]string, n)
109
+ top3 := []string{"Gold Medal", "Silver Medal", "Bronze Medal"}
110
+ for i := 0; i < n; i++ {
111
+ if i < 3 {
112
+ ans[idx[i][1]] = top3[i]
113
+ } else {
114
+ ans[idx[i][1]] = strconv.Itoa(i + 1)
115
+ }
116
+ }
117
+ return ans
118
+ }
119
+ ```
120
+
87
121
### ** ...**
88
122
89
123
```
0 commit comments