Standard Bar Heaps · Medium Problems O(n log n) · O(n)
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.
0 <= arr.length <= 10^5-10^9 <= arr[i] <= 10^9Input: {"arr":[40,10,20,20]}
Output: [3,1,2,2]
Distinct weights in order are 10, 20, 40 — the two 20s share rank 2.
Input: {"arr":[100,100,100]}
Output: [1,1,1]
One distinct weight means every crate is rank 1.