[LeetCode&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: ["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.
class Solution(object):
    def findRelativeRanks(self, nums):
        """
        :type nums: List[int]
        :rtype: List[str]
        """
        n=len(nums)
        if n==0:
            return nums
        elif n==1:
            return [‘Gold Medal‘]
        elif n==2:
            if nums[0]>nums[1]:
                return [‘Gold Medal‘,‘Silver Medal‘]
            else:
                return [‘Silver Medal‘,‘Gold Medal‘]
        ans=[]
        s=sorted(nums)
        a1=s[-1]
        a2=s[-2]
        a3=s[-3]

        for i in nums:
            if i==a1:
                ans.append(‘Gold Medal‘)
            elif i==a2:
                ans.append(‘Silver Medal‘)
            elif i==a3:
                ans.append(‘Bronze Medal‘)
            else:
                ans.append(str(n-s.index(i)))
        return ans

  

原文地址:https://www.cnblogs.com/chiyeung/p/10074727.html

时间: 2024-08-30 11:06:03

[LeetCode&Python] Problem 506. Relative Ranks的相关文章

【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&amp;Python] Problem 905: Sort Array By Parity

Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A. You may return any answer array that satisfies this condition. Example 1: Input: [3,1,2,4] Output: [2,4,3,1] T

[LeetCode&amp;Python] Problem 806. Number of Lines To Write String

We are to write the letters of a given string S, from left to right into lines. Each line has maximum width 100 units, and if writing a letter would cause the width of the line to exceed 100 units, it is written on the next line. We are given an arra

[LeetCode&amp;Python] Problem 811. Subdomain Visit Count

A website domain like "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com", and at the lowest level, "discuss.leetcode.com". When we visit

[LeetCode&amp;Python] Problem 682. Baseball Game

You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 following types: Integer (one round's score): Directly represents the number of points you get in this round. "+" (one round's score): Represents

[LeetCode&amp;Python] Problem 520. Detect Capital

Given a word, you need to judge whether the usage of capitals in it is right or not. We define the usage of capitals in a word to be right when one of the following cases holds: All letters in this word are capitals, like "USA". All letters in t

[LeetCode&amp;Python] Problem 427. Construct Quad Tree

We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or false. The root node represents the whole grid. For each node, it will be subdivided into four children nodes until the values in the region it repres

[LeetCode&amp;Python] Problem 771: Jewels and Stones

You're given strings J representing the types of stones that are jewels, and S representing the stones you have.  Each character in Sis a type of stone you have.  You want to know how many of the stones you have are also jewels. The letters in J are

[LeetCode&amp;Python] Problem 832. Flipping an Image

Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image. To flip an image horizontally means that each row of the image is reversed.  For example, flipping [1, 1, 0] horizontally results in [0,