Problem 264212 · medium · Phase 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 = [8, 1, 2, 2, 3]
Output: [4, 0, 1, 1, 3]
Explanation: 8 beats 1, 2, 2, 3; each 2 beats only the 1.

Input:  nums = [6, 5, 4, 8]
Output: [2, 1, 0, 3]

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…