From 257b503f8afcaad6735f8e547ee0122b406cd2cf Mon Sep 17 00:00:00 2001 From: tphyhFighting <2363176358@qq.com> Date: Thu, 2 Dec 2021 09:58:05 +0800 Subject: [PATCH 1/3] add: leetcode 0506 solution --- .../0506.Relative-Ranks/506.Relative Ranks.go | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 leetcode/0506.Relative-Ranks/506.Relative Ranks.go diff --git a/leetcode/0506.Relative-Ranks/506.Relative Ranks.go b/leetcode/0506.Relative-Ranks/506.Relative Ranks.go new file mode 100644 index 00000000..501b933b --- /dev/null +++ b/leetcode/0506.Relative-Ranks/506.Relative Ranks.go @@ -0,0 +1,29 @@ +package leetcode + +import ( + "sort" + "strconv" +) + +func findRelativeRanks(score []int) []string { + mp := make(map[int]int) + for i, v := range score { + mp[v] = i + } + sort.Slice(score, func(i, j int) bool { + return score[i] > score[j] + }) + ans := make([]string, len(score)) + for i, v := range score { + if i == 0 { + ans[mp[v]] = "Gold Medal" + } else if i == 1 { + ans[mp[v]] = "Silver Medal" + } else if i == 2 { + ans[mp[v]] = "Bronze Medal" + } else { + ans[mp[v]] = strconv.Itoa(i + 1) + } + } + return ans +} From 1fa8edd360a4fcdf46f5a79bbc185665a423eaeb Mon Sep 17 00:00:00 2001 From: tphyhFighting <2363176358@qq.com> Date: Thu, 2 Dec 2021 09:58:16 +0800 Subject: [PATCH 2/3] add: leetcode 0506 test --- .../506.Relative Ranks_test.go | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 leetcode/0506.Relative-Ranks/506.Relative Ranks_test.go diff --git a/leetcode/0506.Relative-Ranks/506.Relative Ranks_test.go b/leetcode/0506.Relative-Ranks/506.Relative Ranks_test.go new file mode 100644 index 00000000..b26017d6 --- /dev/null +++ b/leetcode/0506.Relative-Ranks/506.Relative Ranks_test.go @@ -0,0 +1,45 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question506 struct { + para506 + ans506 +} + +// para 是参数 +type para506 struct { + score []int +} + +// ans 是答案 +type ans506 struct { + ans []string +} + +func Test_Problem506(t *testing.T) { + + qs := []question506{ + + { + para506{[]int{5, 4, 3, 2, 1}}, + ans506{[]string{"Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"}}, + }, + + { + para506{[]int{10, 3, 8, 9, 4}}, + ans506{[]string{"Gold Medal", "5", "Bronze Medal", "Silver Medal", "4"}}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 506------------------------\n") + + for _, q := range qs { + _, p := q.ans506, q.para506 + fmt.Printf("【input】:%v 【output】:%v\n", p.score, findRelativeRanks(p.score)) + } + fmt.Printf("\n\n\n") +} From 752256a7032e4421d6dcfecb220aeae09f6cfac4 Mon Sep 17 00:00:00 2001 From: tphyhFighting <2363176358@qq.com> Date: Thu, 2 Dec 2021 09:58:29 +0800 Subject: [PATCH 3/3] add: leetcode 0506 readme --- leetcode/0506.Relative-Ranks/README.md | 84 ++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 leetcode/0506.Relative-Ranks/README.md diff --git a/leetcode/0506.Relative-Ranks/README.md b/leetcode/0506.Relative-Ranks/README.md new file mode 100644 index 00000000..f679703a --- /dev/null +++ b/leetcode/0506.Relative-Ranks/README.md @@ -0,0 +1,84 @@ +# [506. Relative Ranks](https://leetcode-cn.com/problems/relative-ranks/) + +## 题目 + +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: 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**: + + 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]. + +**Constraints:** + +- n == score.length +- 1 <= n <= 10000 +- 0 <= score[i] <= 1000000 +- All the values in score are unique. + +## 题目大意 + +给你一个长度为 n 的整数数组 score ,其中 score[i] 是第 i 位运动员在比赛中的得分。所有得分都 互不相同 。 + +运动员将根据得分 决定名次 ,其中名次第 1 的运动员得分最高,名次第 2 的运动员得分第 2 高,依此类推。运动员的名次决定了他们的获奖情况: + +- 名次第 1 的运动员获金牌 "Gold Medal" 。 +- 名次第 2 的运动员获银牌 "Silver Medal" 。 +- 名次第 3 的运动员获铜牌 "Bronze Medal" 。 +- 从名次第 4 到第 n 的运动员,只能获得他们的名次编号(即,名次第 x 的运动员获得编号 "x")。 + +使用长度为 n 的数组 answer 返回获奖,其中 answer[i] 是第 i 位运动员的获奖情况。 + +## 解题思路 + +- 用map记录原来score中元素对应的坐标,然后对score进行排序,对排序后的元素我们通过map就可以知道它排的名次了 + +## 代码 + +```go +package leetcode + +import ( + "sort" + "strconv" +) + +func findRelativeRanks(score []int) []string { + mp := make(map[int]int) + for i, v := range score { + mp[v] = i + } + sort.Slice(score, func(i, j int) bool { + return score[i] > score[j] + }) + ans := make([]string, len(score)) + for i, v := range score { + if i == 0 { + ans[mp[v]] = "Gold Medal" + } else if i == 1 { + ans[mp[v]] = "Silver Medal" + } else if i == 2 { + ans[mp[v]] = "Bronze Medal" + } else { + ans[mp[v]] = strconv.Itoa(i + 1) + } + } + return ans +} +``` \ No newline at end of file