bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 506. Relative Ranks

506. Relative Ranks

Leetcode Solutions

Sorting and Mapping Approach

  1. Create a list of pairs where each pair consists of a score and its original index.
  2. Sort this list in descending order based on the scores.
  3. Initialize an answer array of strings with the same length as the input score array.
  4. Iterate through the sorted list of pairs.
    • For the top three scores, assign 'Gold Medal', 'Silver Medal', and 'Bronze Medal' respectively.
    • For the rest, assign the rank as a string, which is the 1-based index in the sorted list.
  5. Place these ranks into the answer array at the original indices.
  6. Return the answer array.
UML Thumbnail

Hash Map and Sorting Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...