Binary Search Python Code


  • 0
    R

    well, it is not that efficient I think. I will appreciate it so much if you can give me any color on how to improve it. Thanks in advance

    class Solution(object):
        def arrangeCoins(self, n):
            """
            :type n: int
            :rtype: int
            """
            left, right = 0, n
            
            if n in (0,1):
                return n
            
            while left < right:
                mid = (left + right)/2
                
                if mid*(mid+1)-2*n < 0:
                    left = mid + 1
                    print left
                else:
                    right = mid 
            
            return left if left*(left+1)-2*n <= 0 else left - 1
    

Log in to reply
 

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