Count of smaller numbers after self

November 29, 2022

binary-search

Problem URL: Count of smaller numbers after self

We will make a sorted copy of the nums. Then we iterate over each number, find the index of the number in the sorted copy, and add the number of numbers smaller than the number to the result.

class Solution:
    def countSmaller(self, nums: List[int]) -> List[int]:
        sorted_nums = sorted(nums)
        result = []
        for num in nums:
            index = bisect.bisect_left(sorted_nums, num)
            result.append(index)
            sorted_nums.pop(index)
        return result

Time complexity: O(nlog(n)), n is the length of the nums
Space complexity: O(n)