From 2f92c3eebf1d78a3cc23635cb9f60e57d17fce64 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 8 May 2024 21:44:14 +0800 Subject: [PATCH] Add solution and test-cases for problem 506 --- .../501-600/0506.Relative-Ranks/README.md | 32 +++++++++++-------- .../501-600/0506.Relative-Ranks/Solution.go | 29 +++++++++++++++-- .../0506.Relative-Ranks/Solution_test.go | 13 ++++---- 3 files changed, 51 insertions(+), 23 deletions(-) diff --git a/leetcode/501-600/0506.Relative-Ranks/README.md b/leetcode/501-600/0506.Relative-Ranks/README.md index 624bad991..be542a3c2 100644 --- a/leetcode/501-600/0506.Relative-Ranks/README.md +++ b/leetcode/501-600/0506.Relative-Ranks/README.md @@ -1,28 +1,32 @@ # [506.Relative Ranks][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given an integer array `score` of size `n`, where `score[i]` is the score of the ith athlete in a competition. All the scores are guaranteed to be **unique**. + +The athletes are **placed** based on their scores, where the 1st place athlete has the highest score, the 2nd place athlete has the 2nd highest score, and so on. The placement of each athlete determines their rank: + +- The 1st place athlete's rank is `"Gold Medal"`. +- The 2nd place athlete's rank is `"Silver Medal"`. +- The 3rd place athlete's rank is `"Bronze Medal"`. +- For the 4th place to the nth place athlete, their rank is their placement number (i.e., the xth place athlete's rank is `"x"`). + +Return an array `answer` of size `n` where `answer[i]` is the **rank** of the ith athlete. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: score = [5,4,3,2,1] +Output: ["Gold Medal","Silver Medal","Bronze Medal","4","5"] +Explanation: The placements are [1st, 2nd, 3rd, 4th, 5th]. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Relative Ranks -```go ``` - +Input: score = [10,3,8,9,4] +Output: ["Gold Medal","5","Bronze Medal","Silver Medal","4"] +Explanation: The placements are [1st, 5th, 3rd, 2nd, 4th]. +``` ## 结语 diff --git a/leetcode/501-600/0506.Relative-Ranks/Solution.go b/leetcode/501-600/0506.Relative-Ranks/Solution.go index d115ccf5e..c0a2fbe73 100644 --- a/leetcode/501-600/0506.Relative-Ranks/Solution.go +++ b/leetcode/501-600/0506.Relative-Ranks/Solution.go @@ -1,5 +1,30 @@ package Solution -func Solution(x bool) bool { - return x +import ( + "sort" + "strconv" +) + +func Solution(score []int) []string { + top := map[int]string{ + 1: "Gold Medal", + 2: "Silver Medal", + 3: "Bronze Medal", + } + tmp := make([][2]int, len(score)) + for i := 0; i < len(score); i++ { + tmp[i] = [2]int{i, score[i]} + } + sort.Slice(tmp, func(i, j int) bool { + return tmp[i][1] > tmp[j][1] + }) + ans := make([]string, len(score)) + for i := 0; i < len(tmp); i++ { + if v, ok := top[i+1]; ok { + ans[tmp[i][0]] = v + continue + } + ans[tmp[i][0]] = strconv.Itoa(i + 1) + } + return ans } diff --git a/leetcode/501-600/0506.Relative-Ranks/Solution_test.go b/leetcode/501-600/0506.Relative-Ranks/Solution_test.go index 14ff50eb4..87b8e0825 100644 --- a/leetcode/501-600/0506.Relative-Ranks/Solution_test.go +++ b/leetcode/501-600/0506.Relative-Ranks/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect []string }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{5, 4, 3, 2, 1}, []string{"Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"}}, + {"TestCase2", []int{10, 3, 8, 9, 4}, []string{"Gold Medal", "5", "Bronze Medal", "Silver Medal", "4"}}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }