6 line python solution using bisect


  • 1
    M
    import bisect
    def countSmaller(self, nums):
        counts, sortednums = [], []
        for i in range(len(nums)-1,-1, -1):
            position = bisect.bisect_left(sortednums, nums[i])
            sortednums.insert(position, nums[i])
            counts.append(position)
        return counts[::-1]
    

    To learn more about how bisect works: https://pymotw.com/2/bisect/


Log in to reply
 

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