fbaquant / LeetCode

1 stars 0 forks source link

How Many Numbers Are Smaller Than the Current Number #122

Open juneharold opened 3 months ago

juneharold commented 3 months ago

Sort the array. Then the procedure is trivial.

class Solution:
   def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
       # [1,2,2,3,8]
       # [0,1,2,3,4]
       sorted_nums = sorted(nums)
       mapping = {}
       for i, num in enumerate(sorted_nums):
           if num not in mapping:
               mapping[num] = i
       return [mapping[num] for num in nums]