Relative Ranks

You are given an integer array score of size n, where score[i] is the score of the ith athlete. All scores are unique. Athletes are placed by score: the highest score is 1st, the next is 2nd, and so on. The 1st place athlete gets "Gold Medal", 2nd gets "Silver Medal", 3rd gets "Bronze Medal", and from 4th place on the rank is the placement number as a string. Return an array answer where answer[i] is the rank of the ith athlete.

30:00

Relative Ranks
easy
Free written solution
Topics
Companies

You are given an integer array score of size n, where score[i] is the score of the ith athlete. All scores are unique. Athletes are placed by score: the highest score is 1st, the next is 2nd, and so on. The 1st place athlete gets "Gold Medal", 2nd gets "Silver Medal", 3rd gets "Bronze Medal", and from 4th place on the rank is the placement number as a string. Return an array answer 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"]
Constraints:
  • n==score.lengthn == \text{score.length}

  • 1≤n≤1041 \leq n \leq 10^4

  • 0≤score[i]≤1060 \leq \text{score}[i] \leq 10^6

  • All the values in score are unique.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"score":[5,4,3,2,1]}

5 athletes with unique scores. Rank them by score, but write each rank back at the athlete's own index.

athletes (score) and answer[i]

i=0

5

?

i=1

4

?

i=2

3

?

i=3

2

?

i=4

1

?

max-heap by score

size 0

empty

podium (pop order)

Silver

Gold

Bronze

Variables
VariableValue
n5
heapSize0
DepthFunction Call
Stack empty
0/18