Count of Smaller Numbers After Self
Given an integer array nums, return a new array counts where counts[i] is the number of elements to the right of nums[i] that are strictly smaller than nums[i].
Open official problem prompt ↗For every position, count how many values appearing later in the array are strictly smaller than it.
Walk backward through a line of people, keeping a tally sheet bucketed by height. Before adding yourself, glance at the sheet and sum every bucket shorter than you — that is how many shorter people already stand ahead (to your right).
- Input
- nums = [5,2,6,1]
- Output
- [2,1,1,0]
- Why
- Right of 5 are {2,1} smaller (2); right of 2 is {1} (1); right of 6 is {1} (1); right of 1 nothing (0).
1 <= nums.length <= 10^5-10^4 <= nums[i] <= 10^4