← DiffPush

Rank Transform of an Array

Standard Bar Heaps · Medium Problems O(n log n) · O(n)

The Freight Weight Percentile Stamp

A freight yard stamps each incoming crate with a density rank: the lightest crate in the batch gets rank 1, the next distinct weight rank 2, and so on — crates sharing a weight share a rank. The stamping station processes the batch in weight order and writes each rank back onto the crate's original position on the conveyor.

Input: An integer array arr of crate weights.

Output: Return an array where entry i is the rank of arr[i]: its 1-based position among the sorted distinct values.

Constraints

Examples

Example 1

Input: {"arr":[40,10,20,20]}
Output: [3,1,2,2]
Distinct weights in order are 10, 20, 40 — the two 20s share rank 2.

Example 2

Input: {"arr":[100,100,100]}
Output: [1,1,1]
One distinct weight means every crate is rank 1.

Solve this in your browser →

Also on LeetCode ↗