Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f549a0e

Browse files
committed
feat: add solutions to lc problem: No.0506
No.0506.Relative Ranks
1 parent ad45f3f commit f549a0e

File tree

6 files changed

+171
-80
lines changed

6 files changed

+171
-80
lines changed

‎solution/0500-0599/0506.Relative Ranks/README.md‎

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,11 @@ class Solution:
4141
n = len(score)
4242
idx = list(range(n))
4343
idx.sort(key=lambda x: -score[x])
44-
res = [None] * n
44+
top3 = ['Gold Medal', 'Silver Medal', 'Bronze Medal']
45+
ans = [None] * n
4546
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
5549
```
5650

5751
### **Java**
@@ -60,30 +54,70 @@ class Solution:
6054

6155
```java
6256
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];
6660
for (int i = 0; i < n; ++i) {
67-
index[i] = i;
61+
idx[i] = i;
6862
}
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"};
7166
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);
8168
}
82-
return res;
69+
return ans;
8370
}
8471
}
8572
```
8673

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+
87121
### **...**
88122

89123
```

‎solution/0500-0599/0506.Relative Ranks/README_EN.md‎

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
<li>All the values in <code>score</code> are <strong>unique</strong>.</li>
4545
</ul>
4646

47-
4847
## Solutions
4948

5049
<!-- tabs:start -->
@@ -57,47 +56,81 @@ class Solution:
5756
n = len(score)
5857
idx = list(range(n))
5958
idx.sort(key=lambda x: -score[x])
60-
res = [None] * n
59+
top3 = ['Gold Medal', 'Silver Medal', 'Bronze Medal']
60+
ans = [None] * n
6161
for i in range(n):
62-
if i == 0:
63-
res[idx[i]] = 'Gold Medal'
64-
elif i == 1:
65-
res[idx[i]] = 'Silver Medal'
66-
elif i == 2:
67-
res[idx[i]] = 'Bronze Medal'
68-
else:
69-
res[idx[i]] = str(i + 1)
70-
return res
62+
ans[idx[i]] = top3[i] if i < 3 else str(i + 1)
63+
return ans
7164
```
7265

7366
### **Java**
7467

7568
```java
7669
class Solution {
77-
public String[] findRelativeRanks(int[] nums) {
78-
int n = nums.length;
79-
Integer[] index = new Integer[n];
70+
public String[] findRelativeRanks(int[] score) {
71+
int n = score.length;
72+
Integer[] idx = new Integer[n];
8073
for (int i = 0; i < n; ++i) {
81-
index[i] = i;
74+
idx[i] = i;
8275
}
83-
Arrays.sort(index, (o1, o2) -> Integer.compare(nums[o2], nums[o1]));
84-
String[] res = new String[n];
76+
Arrays.sort(idx, (i1, i2) -> score[i2] - score[i1]);
77+
String[] ans = new String[n];
78+
String[] top3 = new String[]{"Gold Medal", "Silver Medal", "Bronze Medal"};
8579
for (int i = 0; i < n; ++i) {
86-
if (i == 0) {
87-
res[index[i]] = "Gold Medal";
88-
} else if (i == 1) {
89-
res[index[i]] = "Silver Medal";
90-
} else if (i == 2) {
91-
res[index[i]] = "Bronze Medal";
92-
} else {
93-
res[index[i]] = String.valueOf(i + 1);
94-
}
80+
ans[idx[i]] = i < 3 ? top3[i] : String.valueOf(i + 1);
9581
}
96-
return res;
82+
return ans;
9783
}
9884
}
9985
```
10086

87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
vector<string> findRelativeRanks(vector<int> &score) {
93+
int n = score.size();
94+
vector<pair<int, int>> idx;
95+
for (int i = 0; i < n; ++i)
96+
idx.push_back(make_pair(score[i], i));
97+
sort(idx.begin(), idx.end(),
98+
[&](const pair<int, int> &x, const pair<int, int> &y)
99+
{ return x.first > y.first; });
100+
vector<string> ans(n);
101+
vector<string> top3 = {"Gold Medal", "Silver Medal", "Bronze Medal"};
102+
for (int i = 0; i < n; ++i)
103+
ans[idx[i].second] = i < 3 ? top3[i] : to_string(i + 1);
104+
return ans;
105+
}
106+
};
107+
```
108+
109+
### **Go**
110+
111+
```go
112+
func findRelativeRanks(score []int) []string {
113+
n := len(score)
114+
idx := make([][]int, n)
115+
for i := 0; i < n; i++ {
116+
idx[i] = []int{score[i], i}
117+
}
118+
sort.Slice(idx, func(i1, i2 int) bool {
119+
return idx[i1][0] > idx[i2][0]
120+
})
121+
ans := make([]string, n)
122+
top3 := []string{"Gold Medal", "Silver Medal", "Bronze Medal"}
123+
for i := 0; i < n; i++ {
124+
if i < 3 {
125+
ans[idx[i][1]] = top3[i]
126+
} else {
127+
ans[idx[i][1]] = strconv.Itoa(i + 1)
128+
}
129+
}
130+
return ans
131+
}
132+
```
133+
101134
### **...**
102135

103136
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<string> findRelativeRanks(vector<int> &score) {
4+
int n = score.size();
5+
vector<pair<int, int>> idx;
6+
for (int i = 0; i < n; ++i)
7+
idx.push_back(make_pair(score[i], i));
8+
sort(idx.begin(), idx.end(),
9+
[&](const pair<int, int> &x, const pair<int, int> &y)
10+
{ return x.first > y.first; });
11+
vector<string> ans(n);
12+
vector<string> top3 = {"Gold Medal", "Silver Medal", "Bronze Medal"};
13+
for (int i = 0; i < n; ++i)
14+
ans[idx[i].second] = i < 3 ? top3[i] : to_string(i + 1);
15+
return ans;
16+
}
17+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func findRelativeRanks(score []int) []string {
2+
n := len(score)
3+
idx := make([][]int, n)
4+
for i := 0; i < n; i++ {
5+
idx[i] = []int{score[i], i}
6+
}
7+
sort.Slice(idx, func(i1, i2 int) bool {
8+
return idx[i1][0] > idx[i2][0]
9+
})
10+
ans := make([]string, n)
11+
top3 := []string{"Gold Medal", "Silver Medal", "Bronze Medal"}
12+
for i := 0; i < n; i++ {
13+
if i < 3 {
14+
ans[idx[i][1]] = top3[i]
15+
} else {
16+
ans[idx[i][1]] = strconv.Itoa(i + 1)
17+
}
18+
}
19+
return ans
20+
}
Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
class Solution {
2-
public String[] findRelativeRanks(int[] nums) {
3-
int n = nums.length;
4-
Integer[] index = new Integer[n];
2+
public String[] findRelativeRanks(int[] score) {
3+
int n = score.length;
4+
Integer[] idx = new Integer[n];
55
for (int i = 0; i < n; ++i) {
6-
index[i] = i;
6+
idx[i] = i;
77
}
8-
Arrays.sort(index, (o1, o2) -> Integer.compare(nums[o2], nums[o1]));
9-
String[] res = new String[n];
8+
Arrays.sort(idx, (i1, i2) -> score[i2] - score[i1]);
9+
String[] ans = new String[n];
10+
String[] top3 = new String[]{"Gold Medal", "Silver Medal", "Bronze Medal"};
1011
for (int i = 0; i < n; ++i) {
11-
if (i == 0) {
12-
res[index[i]] = "Gold Medal";
13-
} else if (i == 1) {
14-
res[index[i]] = "Silver Medal";
15-
} else if (i == 2) {
16-
res[index[i]] = "Bronze Medal";
17-
} else {
18-
res[index[i]] = String.valueOf(i + 1);
19-
}
12+
ans[idx[i]] = i < 3 ? top3[i] : String.valueOf(i + 1);
2013
}
21-
return res;
14+
return ans;
2215
}
23-
}
16+
}

‎solution/0500-0599/0506.Relative Ranks/Solution.py‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,8 @@ def findRelativeRanks(self, score: List[int]) -> List[str]:
33
n = len(score)
44
idx = list(range(n))
55
idx.sort(key=lambda x: -score[x])
6-
res = [None] * n
6+
top3 = ['Gold Medal', 'Silver Medal', 'Bronze Medal']
7+
ans = [None] * n
78
for i in range(n):
8-
if i == 0:
9-
res[idx[i]] = 'Gold Medal'
10-
elif i == 1:
11-
res[idx[i]] = 'Silver Medal'
12-
elif i == 2:
13-
res[idx[i]] = 'Bronze Medal'
14-
else:
15-
res[idx[i]] = str(i + 1)
16-
return res
9+
ans[idx[i]] = top3[i] if i < 3 else str(i + 1)
10+
return ans

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /