Leetcode: 315. Count of Smaller Numbers After Self

Problem Statement

from typing import List
from sortedcontainers import SortedList


class Solution:
    def countSmaller(self, nums: List[int]) -> List[int]:
        right = SortedList([])
        N = len(nums)
        ans = [0] * N
        for i in range(N - 1, -1, -1):
            j = right.bisect_left(nums[i])
            ans[i] = j
            right.add(nums[i])
        return ans


assert Solution().countSmaller([5, 2, 6, 1]) == [2, 1, 1, 0]
assert Solution().countSmaller([-1]) == [0]
assert Solution().countSmaller([-1, -1]) == [0, 0]