Leetcode Problem 1331. Rank Transform of an Array

1331. Rank Transform of an Array

Leetcode Solutions

Sorting and Mapping Approach

  1. Create a copy of the original array and sort it.
  2. Initialize a hashmap to store the value-to-rank mapping.
  3. Initialize a variable rank to 1.
  4. Iterate through the sorted array and for each element: a. If the element is not in the hashmap, add it with the current rank. b. If the element is a new value (not equal to the previous one), increment rank.
  5. Iterate through the original array and replace each element with its rank from the hashmap.
  6. Return the modified original array.
UML Thumbnail

Direct Ranking with Sorting

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...