Skip to content

Commit 320463b

Browse files
authored
Merge pull request #874 from 0xff-dev/506
Add solution and test-cases for problem 506
2 parents e032727 + 2f92c3e commit 320463b

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

leetcode/501-600/0506.Relative-Ranks/README.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
# [506.Relative Ranks][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an integer array `score` of size `n`, where `score[i]` is the score of the i<sup>th</sup> athlete in a competition. All the scores are guaranteed to be **unique**.
5+
6+
The athletes are **placed** based on their scores, where the 1<sup>st</sup> place athlete has the highest score, the 2<sup>nd</sup> place athlete has the 2<sup>nd</sup> highest score, and so on. The placement of each athlete determines their rank:
7+
8+
- The 1<sup>st</sup> place athlete's rank is `"Gold Medal"`.
9+
- The 2<sup>nd</sup> place athlete's rank is `"Silver Medal"`.
10+
- The 3<sup>rd</sup> place athlete's rank is `"Bronze Medal"`.
11+
- For the 4<sup>th</sup> place to the n<sup>th</sup> place athlete, their rank is their placement number (i.e., the x<sup>th</sup> place athlete's rank is `"x"`).
12+
13+
Return an array `answer` of size `n` where `answer[i]` is the **rank** of the i<sup>th</sup> athlete.
714

815
**Example 1:**
916

1017
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
18+
Input: score = [5,4,3,2,1]
19+
Output: ["Gold Medal","Silver Medal","Bronze Medal","4","5"]
20+
Explanation: The placements are [1st, 2nd, 3rd, 4th, 5th].
1321
```
1422

15-
## 题意
16-
> ...
23+
**Example 2:**
1724

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Relative Ranks
23-
```go
2425
```
25-
26+
Input: score = [10,3,8,9,4]
27+
Output: ["Gold Medal","5","Bronze Medal","Silver Medal","4"]
28+
Explanation: The placements are [1st, 5th, 3rd, 2nd, 4th].
29+
```
2630

2731
## 结语
2832

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"sort"
5+
"strconv"
6+
)
7+
8+
func Solution(score []int) []string {
9+
top := map[int]string{
10+
1: "Gold Medal",
11+
2: "Silver Medal",
12+
3: "Bronze Medal",
13+
}
14+
tmp := make([][2]int, len(score))
15+
for i := 0; i < len(score); i++ {
16+
tmp[i] = [2]int{i, score[i]}
17+
}
18+
sort.Slice(tmp, func(i, j int) bool {
19+
return tmp[i][1] > tmp[j][1]
20+
})
21+
ans := make([]string, len(score))
22+
for i := 0; i < len(tmp); i++ {
23+
if v, ok := top[i+1]; ok {
24+
ans[tmp[i][0]] = v
25+
continue
26+
}
27+
ans[tmp[i][0]] = strconv.Itoa(i + 1)
28+
}
29+
return ans
530
}

leetcode/501-600/0506.Relative-Ranks/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect []string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{5, 4, 3, 2, 1}, []string{"Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"}},
17+
{"TestCase2", []int{10, 3, 8, 9, 4}, []string{"Gold Medal", "5", "Bronze Medal", "Silver Medal", "4"}},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)