Problem 264212 · medium · Level 02 Linear Data Structures

Smaller Than Me

arrays · counting · ranking

For each element of nums, count how many other elements are strictly smaller than it. Return the counts in the original order.

Examples

Input:  nums = [9, 4, 6, 4, 2]
Output: [4, 1, 3, 1, 0]
Explanation: 9 beats 4, 6, 4, 2; each 4 beats only the 2.

Input:  nums = [7, 10, 3, 5]
Output: [2, 3, 0, 1]

Constraints

  • 0 <= len(nums) <= 10**5
  • 0 <= nums[i] <= 100
  • Target: O(n + 100) time; comparing every pair is too slow.

Goals

  • Replace an O(n^2) pairwise comparison with counting
  • Use a running total over a small value range
Starting Python…