Python binary search solution


  • 2
    T
    def countSmaller(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        res = []
        s = [] #sorted list
        for n in nums[::-1]:
            i = bisect.bisect_left(s,n)
            #find the index, which means how many numbers in the sorted array are smaller than the cur number
            res.insert(0,i)
            bisect.insort(s,n)
        return res

Log in to reply
 

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