Leetcode 1482 Solution

This article provides solution to leetcode question 1482 (how-many-numbers-are-smaller-than-the-current-number)

https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number

Solution

class Solution:
    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
        a = [(num, i) for i, num in enumerate(nums)]
        a.sort()

        ans = [0] * len(nums)
        for num, i in a:
            l = 0
            r = len(a) - 1
            while l < r:
                m = (l + r) // 2
                if a[m][0] >= num:
                    r = m
                else:
                    l = m + 1
            ans[i] = l
        return ans