2025-08-02 16:12:42 +0000 UTC

How Many Numbers Are Smaller Than the Current Number

Code

class Solution:
    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
        num_to_count = {}
        for i, num in enumerate(sorted(nums)):
            if num not in num_to_count:
                num_to_count[num] = i
        for i in range(len(nums)):
            nums[i] = num_to_count[nums[i]]
        return nums