Ctrl K

Compressed Ranks

Easy
You get an integer array values. Replace every number with its rank and return the resulting array. Ranks work like this: - a rank is an integer starting from 1; - a larger number gets a larger rank, and equal numbers get equal ranks; - ranks are as small as possible, so they go consecutively with no gaps.
Example 1
values=[7, -2, 7, 0] → [3, 1, 3, 2]

The distinct values in increasing order are -2, 0, 7, with ranks 1, 2, 3. Both 7s get rank 3.

Example 2
values=[50, 40, 30] → [3, 2, 1]

The smallest is 30 with rank 1; the largest is 50 with rank 3.

Example 3
values=[] → []
Constraints
  • 0 ≤ len(values) ≤ 10⁵
  • -10⁹ ≤ values[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsThe Candidate With Most Votes EasyDoes Any Value Repeat EasyThe Lost Ticket EasyValues in Both Lists EasyShared Values With Repeats Easy
loading Python…
Input
values=[7, -2, 7, 0]
Expected
[3, 1, 3, 2]

Run checks the first 3 tests in your browser. Submit checks all 115 tests on the server, hidden ones included.