[LeetCode] 506. Relative Ranks_Easy tag: Sort

Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal".

Example 1:

Input: [5, 4, 3, 2, 1]
Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
Explanation: The first three athletes got the top three highest scores, so they got "Gold Medal", "Silver Medal" and "Bronze Medal". For the left two athletes, you just need to output their relative ranks according to their scores.

Note:

  1. N is a positive integer and won‘t exceed 10,000.
  2. All the scores of athletes are guaranteed to be unique.

Code

class Solution:
    def findRelativeRanks(self, nums):
        if len(nums) == 1: return ["Gold Medal"]
        if len(nums) == 2: return ["Gold Medal", "Silver Medal"] if nums[0] > nums[1] else ["Silver Medal", "Gold Medal"]
        ans, d = [0]*len(nums), sorted([(num, index) for index, num in enumerate(nums)], reverse = True)
        ans[d[0][1]], ans[d[1][1]], ans[d[2][1]] =  "Gold Medal", "Silver Medal", "Bronze Medal"
        for i in range(3, len(d)):
            ans[d[i][1]] = str(i+1)
        return ans

原文地址:https://www.cnblogs.com/Johnsonxiong/p/9547446.html

时间: 2024-10-11 12:37:19

[LeetCode] 506. Relative Ranks_Easy tag: Sort的相关文章

[LeetCode] 455. Assign Cookies_Easy tag: Sort

Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each

【leetcode】506. Relative Ranks

problem 506. Relative Ranks solution1:使用优先队列: 掌握priority_queue 和 pair的使用: class Solution { public: vector<string> findRelativeRanks(vector<int>& nums) { priority_queue<pair<int, int>> myqueue;// for(int i=0; i<nums.size(); i

【leetcode刷题笔记】Sort List

Sort a linked list in O(n log n) time using constant space complexity. 题解:实现一个链表的归并排序即可.主要分为三部分: 1.找到中点并返回的函数findMiddle; 2.归并函数merge; 3.排序函数sortList. 数组的findMiddle函数非常容易实现,链表就有一点tricky了.首先设置两个指针,一个slow初始化为head,一个fast初始化为head.next,然后slow一次走一步,fast一次走两

[LeetCode] 130. Surrounded Regions_Medium tag: DFS

Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. Example: X X X X X O O X X X O X X O X X After running your function, the bo

LeetCode 1122. Relative Sort Array (数组的相对排序)

题目标签:Sort 先设立一个 size 1000 的 array cnt [], 把arr1 里面的数字 计数存入 cnt: 遍历 arr2, 按照arr2 的顺序把 arr1 与 arr2 重复的数字 先存入 arr1: 遍历 cnt,把 cnt 里面剩余的 arr1 剩下的数字 存入arr1: 具体看code.  Java Solution: Runtime:  0 ms, faster than 100.00 % Memory Usage: 38 MB, less than 100.00

[LeetCode&amp;Python] Problem 506. Relative Ranks

Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal". Example 1: Input: [5, 4, 3, 2, 1] Output: [&

leetcode 506. 相对名次(Relative Ranks)

目录 题目描述: 示例 1: 解法: 题目描述: 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 "金牌","银牌" 和" 铜牌"("Gold Medal", "Silver Medal", "Bronze Medal"). (注:分数越高的选手,排名越靠前.) 示例 1: 输入: [5, 4, 3, 2, 1] 输出: ["Go

[LeetCode] 90.Subsets II tag: backtracking

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Example: Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 这个题目思路

[LeetCode] 72. Edit Distance_hard tag: Dynamic Programming

Given two words word1 and word2, find the minimum number of operations required to convert word1to word2. You have the following 3 operations permitted on a word: Insert a character Delete a character Replace a character Example 1: Input: word1 = "ho