506-相对名次(代码片段)

angelica-duhurica angelica-duhurica     2023-04-24     612

关键词:

506-相对名次

给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌。前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”("Gold Medal", "Silver Medal", "Bronze Medal")。

(注:分数越高的选手,排名越靠前。)

示例 1:

输入: [5, 4, 3, 2, 1]
输出: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
解释: 前三名运动员的成绩为前三高的,因此将会分别被授予 “金牌”,“银牌”和“铜牌” ("Gold Medal", "Silver Medal" and "Bronze Medal").
余下的两名运动员,我们只需要通过他们的成绩计算将其相对名次即可。

提示:

  1. N 是一个正整数并且不会超过 10000。
  2. 所有运动员的成绩都不相同。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/relative-ranks
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    public String[] findRelativeRanks(int[] nums) 
        // 将原数组和索引数组对应
        int[] pos = new int[nums.length];
        for(int i = 0; i < pos.length; i++) 
            pos[i] = i;
        

        for(int i = 0; i < nums.length; i++) 
            for(int j = i + 1; j < nums.length; j++) 
                if(nums[i] < nums[j]) 
                    // 一起交换
                    exch(nums, i, j);
                    exch(pos, i, j);
                
            
        

        String[] res = new String[pos.length];
        for(int i = 0; i < pos.length; i++) 
            if(i == 0) 
                res[pos[i]] = "Gold Medal";
             else if(i == 1) 
                res[pos[i]] = "Silver Medal";
             else if(i == 2) 
                res[pos[i]] = "Bronze Medal";
             else 
                res[pos[i]] = String.valueOf(i + 1);
            
        
        return res;
    
    private void exch(int[] nums, int i, int j) 
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    
    public String[] findRelativeRanks(int[] nums) 
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < nums.length; i++) 
            // key为原数组,value为索引
            map.put(nums[i], i);
        

        // 对key排序
        Set<Integer> set = new TreeSet<>(map.keySet()).descendingSet();
        Iterator<Integer> iterator = set.iterator();
        int i = 0;
        String[] res = new String[nums.length];
        while (iterator.hasNext()) 
            Integer key = iterator.next();
            if(i == 0) 
                res[map.get(key)] = "Gold Medal";
             else if(i == 1) 
                res[map.get(key)] = "Silver Medal";
             else if(i == 2) 
                res[map.get(key)] = "Bronze Medal";
             else 
                res[map.get(key)] = String.valueOf(i + 1);
            
            i++;

            iterator.remove();
        
        return res;
    
    public String[] findRelativeRanks(int[] nums) 
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < nums.length; i++) 
            // key为原数组,value为索引
            map.put(nums[i], i);
        

        // 对key排序
        Arrays.sort(nums);

        String[] res = new String[nums.length];
        for(int i = nums.length - 1; i >= 0;  i--) 
            Integer key = nums[i];
            if(i == nums.length - 1) 
                res[map.get(key)] = "Gold Medal";
             else if(i == nums.length - 2) 
                res[map.get(key)] = "Silver Medal";
             else if(i == nums.length - 3) 
                res[map.get(key)] = "Bronze Medal";
             else 
                res[map.get(key)] = String.valueOf(nums.length - i);
            
        
        return res;
    
    public String[] findRelativeRanks(int[] nums) 
        int[] x = Arrays.copyOf(nums, nums.length);
        // 对目标数组进行排序
        Arrays.sort(x);
        HashMap<Integer, String> map = new HashMap<>();
        int len = nums.length;
        // 依据排序结果颁发名词并置于哈希表内
        for (int i = 0; i < x.length; i++) 
            if (i == 0) 
                map.put(x[len - 1 - i], "Gold Medal");
             else if (i == 1) 
                map.put(x[len - 1 - i], "Silver Medal");
             else if (i == 2) 
                map.put(x[len - 1 - i], "Bronze Medal");
             else 
                map.put(x[len - 1 - i], Integer.toString(i + 1));
            
        
        String[] res = new String[len];
        for (int i = 0; i < len; i++) 
            res[i] = map.get(nums[i]);
        
        return res;
    

快乐水题506.相对名次(代码片段)

原题:力扣链接:506.相对名次题目简述:给你一个长度为n的整数数组score,其中score[i]是第i位运动员在比赛中的得分。所有得分都互不相同。运动员将根据得分决定名次,其中名次第1的运动员得分最高,名次... 查看详情

506relativeranks相对名次

给出N名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌。前三名运动员将会被分别授予“金牌”,“银牌”和“铜牌”("GoldMedal","SilverMedal","BronzeMedal")。(注:分数越高的选手,排名越靠前。)示例1:输入:[5,4,3,2,1]... 查看详情

leetcode506相对名次[排序]heroding的leetcode之路(代码片段)

解题思路:解决该题的关键就是要让得分与序号形成映射关系,用pair放入vector容器中,直接排序,最后按照排序结果给出获奖情况,代码如下:classSolutionpublic:vector<string>findRelativeRanks(vector<int>&... 查看详情

leetcode1446.连续字符/506.相对名次/1005.k次取反后最大化的数组和(代码片段)

1446.连续字符2021.12.1每日一题,又是新的一月题目描述给你一个字符串s,字符串的「能量」定义为:只包含一种字符的最长非空子字符串的长度。请你返回字符串的能量。示例1:输入:s=“leetcode”输出ÿ... 查看详情

算法42-----相对名次(代码片段)

一、题目:给出 N名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌。前三名运动员将会被分别授予“金牌”,“银牌”和“铜牌”("GoldMedal","SilverMedal","BronzeMedal")。(注:分数越高的选手,排名越靠前。)示例... 查看详情

leetcode506.relativeranks(代码片段)

...唯一,因此先对数组排序,然后构造有序数组与名次对应的字典,然后遍历数组得到对应的名词。Version1classSolution:deffin 查看详情

《leetcode之每日一题》:225.相对名次(代码片段)

相对名次有关题目题解题目链接:相对名次有关题目给你一个长度为n的整数数组score,其中score[i]是第i位运动员在比赛中的得分。所有得分都互不相同。运动员将根据得分决定名次,其中名次第1的运动员得分最高ÿ... 查看详情

算法千题案例每日leetcode打卡——67.相对名次(代码片段)

📢前言🌲原题样例:相对名次🌻C#方法🌻Java方法:排序💬总结📢前言🚀算法题🚀🌲每天打卡一道算法题,既是一个学习过程,又是一个分享的过程😜🌲提示:... 查看详情

算法千题案例每日leetcode打卡——67.相对名次(代码片段)

📢前言🌲原题样例:相对名次🌻C#方法🌻Java方法:排序💬总结📢前言🚀算法题🚀🌲每天打卡一道算法题,既是一个学习过程,又是一个分享的过程😜🌲提示:... 查看详情

uva506-systemdependencies(拓扑序)(代码片段)

Problem UVA506-SystemDependenciesAccept:285 Submit:2824TimeLimit:3000mSecProblemDescription Componentsofcomputersystemsoftenhavedependencies—othercomponentsthatmustbeinstalledbeforethey 查看详情

leetcode506.relativeranks(代码片段)

problem506. RelativeRankssolution1:使用优先队列;掌握priority_queue和pair的使用;classSolutionpublic:vector<string>findRelativeRanks(vector<int>&nums)priority_queue<pair<int,int>> 查看详情

计算名次元素在队列中的名次(代码片段)

 [计算名次]元素在队列中的名次(rank)可定义为队列中所有比它小的元素数目加上在它左边出现的与它相同的元素数目。例如,给定一个数组a=[4,3,9,3,7]作为队列,则各元素的名次为r=[2,0,4,1,3]。函数Rank࿰... 查看详情

haas506快速开始(代码片段)

1、硬件介绍HaaS506是一款经过阿里云HaaS团队认证的高性能、多连接的物联网开发板。HaaS506是一款集成了M6014G-CAT1模块的开发板,板载三组双色LED灯和一个自定义功能按键方便用户调试开发,并且提供丰富的接口用于外设... 查看详情

codeforcesround#506(div.3)c.maximalintersection(代码片段)

C.MaximalIntersectiontimelimitpertest3secondsmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputYouaregiven nn segmentsonanumberline;eachendpointofeverysegmenthasintegercoor 查看详情

codeforcesround#506(div.3)c.maximalintersection(枚举)(代码片段)

【题目描述】Youaregiven$n$segmentsonanumberline;eachendpointofeverysegmenthasintegercoordinates.Somesegmentscandegeneratetopoints.Segmentscanintersectwitheachother,benestedineachotherorevencoincide.Theinter 查看详情

hdu1285确定比赛名次(代码片段)

确定比赛名次TimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):33762    AcceptedSubmission(s):13236ProblemDescription有N个比赛队 查看详情

codeforcesround#506(div.3)d.concatenatedmultiples(代码片段)

D.ConcatenatedMultiplesYouaregivenanarray aa,consistingof nn positiveintegers.Let‘scallaconcatenationofnumbers xx and yy thenumberthatisobtainedbywritingdownnumbers& 查看详情

期末考试之排名次(代码片段)

期末考试之排名次TimeLimit:1000msMemoryLimit:65536KiBSubmitStatisticDiscussProblemDescription期末考试结束了,童鞋们的成绩也出来的了,可是为了排名次可忙坏了老师,因为学生太多了。这时,老师把这个任务交给了你,希望你能帮老师完成... 查看详情