排序题目:相对名次

题目

标题和出处

标题:相对名次

出处:506. 相对名次

难度

3 级

题目描述

要求

给定一个长度为 n \texttt{n} n 的整数数组 score \texttt{score} score,其中 score[i] \texttt{score[i]} score[i] 是第 i \texttt{i} i 位运动员在比赛中的得分。所有得分都各不相同

运动员将根据得分决定名次,其中名次第 1 \texttt{1} 1 的运动员得分最高,名次第 2 \texttt{2} 2 的运动员得分第 2 \texttt{2} 2 高,以此类推。运动员的名次决定了他们的获奖情况:

  • 名次第 1 \texttt{1} 1 的运动员获金牌 "Gold   Medal" \texttt{"Gold Medal"} "Gold Medal"
  • 名次第 2 \texttt{2} 2 的运动员获银牌 "Silver   Medal" \texttt{"Silver Medal"} "Silver Medal"
  • 名次第 3 \texttt{3} 3 的运动员获铜牌 "Bronze   Medal" \texttt{"Bronze Medal"} "Bronze Medal"
  • 从名次第 4 \texttt{4} 4 到第 n \texttt{n} n 的运动员获得他们的名次编号(即,名次第 x \texttt{x} x 的运动员获得编号 "x" \texttt{"x"} "x")。

返回长度为 n \texttt{n} n 的数组 answer \texttt{answer} answer,其中 answer[i] \texttt{answer[i]} answer[i] 是第 i \texttt{i} i 位运动员的获奖情况。

示例

示例 1:

输入: score   =   [5,4,3,2,1] \texttt{score = [5,4,3,2,1]} score = [5,4,3,2,1]
输出: ["Gold   Medal","Silver   Medal","Bronze   Medal","4","5"] \texttt{["Gold Medal","Silver Medal","Bronze Medal","4","5"]} ["Gold Medal","Silver Medal","Bronze Medal","4","5"]
解释:名次为 [1 st ,   2 nd ,   3 rd ,   4 th ,   5 th ] \texttt{[1}^\texttt{st}\texttt{, 2}^\texttt{nd}\texttt{, 3}^\texttt{rd}\texttt{, 4}^\texttt{th}\texttt{, 5}^\texttt{th}\texttt{]} [1st, 2nd, 3rd, 4th, 5th]

示例 2:

输入: score   =   [10,3,8,9,4] \texttt{score = [10,3,8,9,4]} score = [10,3,8,9,4]
输出: ["Gold   Medal","5","Bronze   Medal","Silver   Medal","4"] \texttt{["Gold Medal","5","Bronze Medal","Silver Medal","4"]} ["Gold Medal","5","Bronze Medal","Silver Medal","4"]
解释:名次为 [1 st ,   5 th ,   3 rd ,   2 nd ,   4 th ] \texttt{[1}^\texttt{st}\texttt{, 5}^\texttt{th}\texttt{, 3}^\texttt{rd}\texttt{, 2}^\texttt{nd}\texttt{, 4}^\texttt{th}\texttt{]} [1st, 5th, 3rd, 2nd, 4th]

数据范围

  • n = score.length \texttt{n} = \texttt{score.length} n=score.length
  • 1 ≤ n ≤ 10 4 \texttt{1} \le \texttt{n} \le \texttt{10}^\texttt{4} 1n104
  • 0 ≤ score[i] ≤ 10 6 \texttt{0} \le \texttt{score[i]} \le \texttt{10}^\texttt{6} 0score[i]106
  • score \texttt{score} score 中的所有值各不相同

解法

思路和算法

为了得到 n n n 位运动员的相对名次,需要将运动员按照得分降序排序,排序的同时需要保留运动员的下标信息。因此需要创建一个二维数组,二维数组有 n n n 2 2 2 列,每一行分别记录每一位运动员的得分与该运动员在原始数组中的下标。

对二维数组按照得分降序排序,排序后的二维数组中,第 i i i 行的下标信息表示排在第 i + 1 i + 1 i+1 名的运动员在原始数组中的下标位置( 0 ≤ i < n 0 \le i < n 0i<n,名次从 1 1 1 开始因此第 i i i 行对应第 i + 1 i + 1 i+1 名)。

遍历排序后的二维数组,得到每个名次对应的运动员在原始数组中的下标位置,将该名次对应的获奖情况填入结果数组中的相应下标位置。

代码

class Solution {
    public String[] findRelativeRanks(int[] score) {
        int n = score.length;
        int[][] scoreIndex = new int[n][2];
        for (int i = 0; i < n; i++) {
            scoreIndex[i][0] = score[i];
            scoreIndex[i][1] = i;
        }
        Arrays.sort(scoreIndex, (a, b) -> b[0] - a[0]);
        String[] answer = new String[n];
        for (int i = 0; i < n; i++) {
            int index = scoreIndex[i][1];
            answer[index] = getRank(i + 1);
        }
        return answer;
    }

    public String getRank(int num) {
        switch (num) {
        case 1:
            return "Gold Medal";
        case 2:
            return "Silver Medal";
        case 3:
            return "Bronze Medal";
        default:
            return String.valueOf(num);
        }
    }
}

复杂度分析

  • 时间复杂度: O ( n log ⁡ n ) O(n \log n) O(nlogn),其中 n n n 是数组 score \textit{score} score 的长度。需要创建长度为 n n n 的二维数组并排序,排序需要 O ( n log ⁡ n ) O(n \log n) O(nlogn) 的时间,排序后遍历需要 O ( n ) O(n) O(n) 的时间,因此时间复杂度是 O ( n log ⁡ n ) O(n \log n) O(nlogn)

  • 空间复杂度: O ( n ) O(n) O(n),其中 n n n 是数组 score \textit{score} score 的长度。需要创建长度为 n n n 的二维数组并排序。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伟大的车尔尼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值