[LeetCode] 506. 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.

相对名次。

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

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

名次第 1 的运动员获金牌 “Gold Medal” 。
名次第 2 的运动员获银牌 “Silver Medal” 。
名次第 3 的运动员获铜牌 “Bronze Medal” 。
从名次第 4 到第 n 的运动员,只能获得他们的名次编号(即,名次第 x 的运动员获得编号 “x”)。
使用长度为 n 的数组 answer 返回获奖,其中 answer[i] 是第 i 位运动员的获奖情况。

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

思路一 - bucket sort

首先找到 input 中最大的元素,这个最大的元素是最大的 score 分数,这样才能知道桶排序里面到底需要多少个桶。创建了桶之后,可以从右往左扫描桶,也就是从高分往低分扫,如果找到某个 index,他在原数组的相同 index 里面是有数字的,则给他 assign medal 或者数字。注意前三个人分配到奖牌之后,从第四高的分数开始,我们是分配一个相对的名次。

复杂度

时间O(n)
空间O(n)

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public String[] findRelativeRanks(int[] score) {
int max = -1;
for (int s : score) {
max = Math.max(max, s);
}

int n = score.length;
String[] res = new String[n];
int[] bucket = new int[max + 1];
for (int i = 0; i < n; i++) {
bucket[score[i]] = i + 1;
}

int rank = 1;
for (int i = bucket.length - 1; i >= 0; i--) {
if (bucket[i] != 0) {
if (rank == 1) {
res[bucket[i] - 1] = "Gold Medal";
} else if (rank == 2) {
res[bucket[i] - 1] = "Silver Medal";
} else if (rank == 3) {
res[bucket[i] - 1] = "Bronze Medal";
} else {
res[bucket[i] - 1] = String.valueOf(rank);
}
rank++;
}
}
return res;
}
}

思路二 - 堆

创建一个最大堆,把 input 数组里的每个 index 和 score 做成 [index, score] 放入最大堆,堆里的元素是按照 score 降序排列的。然后从堆中开始取出元素,取出的前三个元素就是得金银铜牌的运动员。

复杂度

时间O(nlogn)
空间O(n)

代码

Java实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public String[] findRelativeRanks(int[] score) {
PriorityQueue<int[]> queue = new PriorityQueue<>((a, b) -> b[1] - a[1]);
int n = score.length;
for (int i = 0; i < n; i++) {
queue.offer(new int[] { i, score[i] });
}

String[] res = new String[n];
int rank = 1;
while (!queue.isEmpty()) {
int[] cur = queue.poll();
int index = cur[0];
int s = cur[1];
if (rank == 1) {
res[index] = "Gold Medal";
rank++;
} else if (rank == 2) {
res[index] = "Silver Medal";
rank++;
} else if (rank == 3) {
res[index] = "Bronze Medal";
rank++;
} else {
res[index] = String.valueOf(rank);
rank++;
}
}
return res;
}
}

[LeetCode] 506. Relative Ranks
https://shurui91.github.io/posts/3961220199.html
Author
Aaron Liu
Posted on
January 14, 2020
Licensed under