Given an integer array arr, replace every element with its rank. The smallest value gets rank 1, the next larger distinct value gets rank 2, and so on. Equal values share the same rank, and ranks are as small as possible.
Given an integer array arr, replace every element with its rank. The smallest value gets rank 1, the next larger distinct value gets rank 2, and so on. Equal values share the same rank, and ranks are as small as possible.
Example 1:
Input: {"arr":[40,10,20,30]}
Output: [4,1,2,3]
Constraints:
0≤arr.length≤105
−109≤arr[i]≤109
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"arr":[40,10,20,30]}
A value's rank is its position among the distinct values in sorted order. Build a value-to-rank map, then look each element up.
arr (original order)
.
40
.
10
.
20
.
30
rank staircase: one step per distinct value (0 of 4)