7 line python easy and fast

  • 0

    Finding numbers on the right that is smaller is equivalent to doing binary search in reverse order. Used bisect lib to simplify code.

    def countSmaller(self, nums):
        ret, arr = [], []
        for i in range(len(nums)-1,-1,-1):
            index = bisect.bisect_left(arr, nums[i])
            bisect.insort(arr, nums[i])
        return ret[::-1]


Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.