首页 > 其他分享 >leetcode-506-easy

leetcode-506-easy

时间:2022-11-20 10:01:35浏览次数:62  
标签:map athlete rank score place easy 506 Medal leetcode

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 <= 104
0 <= score[i] <= 106
All the values in score are unique.

思路一:排序,用 map 存储对应的 rank

public String[] findRelativeRanks(int[] score) {
    String[] result = new String[score.length];
    int[] clone = new int[score.length];
    System.arraycopy(score, 0, clone, 0, score.length);
    Arrays.sort(score);
    Map<Integer, String> map = new HashMap<>();
    int rank = 1;
    for (int i = score.length - 1; i >= 0; i--) {
        map.put(score[i], String.valueOf(rank));

        if (rank ==  1) {
            map.put(score[i], "Gold Medal");
        }
        if (rank == 2) {
            map.put(score[i], "Silver Medal");
        }
        if (rank == 3) {
            map.put(score[i], "Bronze Medal");
        }
        rank++;
    }

    for (int i = 0; i < result.length; i++) {
        result[i] = map.get(clone[i]);
    }

    return result;
}

标签:map,athlete,rank,score,place,easy,506,Medal,leetcode
From: https://www.cnblogs.com/iyiluo/p/16907919.html

相关文章

  • 代码随想录day4---LeetCode24. 两两交换链表中的节点&19.删除链表的倒数第N个节点&面
    LeetCode24.两两交换链表中的节点题目链接给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节......
  • EasyX之鼠标
    一、头文件#include<graphics.h>二、鼠标功能以下截取自graphics.h//Oldmouserelatedfunctions旧鼠标相关功能//Mousemessage//WM_MOUSEMOVE......
  • #yyds干货盘点# LeetCode 腾讯精选练习 50 题:2 的幂
    题目:给你一个整数n,请你判断该整数是否是2的幂次方。如果是,返回true;否则,返回false。如果存在一个整数x使得 n==2x,则认为n是2的幂次方。 示例1:输入:n=1输......
  • leetcode_Day1_14最长公共前缀
    1.题目  2.解一  主要思路:横向比较,字符串数组的公共前缀等于前两个字符串的公共前缀与第三个字符串比较,再与第四个比较。即依次遍历字符串数组中的每个字符串,对......
  • [LeetCode] 1099. Two Sum Less Than K
    Givenanarraynumsofintegersand integerk,returnthemaximumsumsuchthatthereexistsi<jwithnums[i]+nums[j]=sumandsum<k.Ifnoi,jexis......
  • [LeetCode] 891. Sum of Subsequence Widths
    Thewidthofasequenceisthedifferencebetweenthemaximumandminimumelementsinthesequence.Givenanarrayofintegersnums,returnthesumofthewi......
  • [oeasy]python0017_解码_decode_字节序列_bytes_字符串_str
    ​ 解码decode回忆上次内容code就是码最早也指电报码后来有各种编码、密码、砝码、条码都指的是把各种事物编个号encode就是编码编码就是给事物编个号......
  • 代码随想录day3---LeetCode203移除链表元素&707设计链表&206反转链表
    LeetCode203移除链表元素给你一个链表的头节点head和一个整数val,请你删除链表中所有满足Node.val==val的节点,并返回新的头节点。示例1:输入:head=[1,2,6,......
  • BUUCTF之2019红帽杯--easyre
    老样子,先查壳无壳,ELF64位,丢IDA继续分析即可首先看下字符串表,发现有一大串字符,还有很像Base64索引表的字符串来到主要逻辑函数首先发现,v12,v13,v14是一个大数组,不过......
  • leetcode611
    有效三角形的个数Category Difficulty Likes Dislikesalgorithms Medium(53.27%) 447 -TagsCompanies给定一个包含非负整数的数组nums,返回其中可以组成三角形三条......