Straightforward PYTHON


  • 0
    V
    class Solution(object):
        def arrangeCoins(self, n):
            """
            :type n: int
            :rtype: int
            """
            if n == 0: return 0
            if n == 1: return 1
            
            lo = 1
            hi = n
    
            while lo <= hi:
                mid = (lo + hi) / 2
                testsum = ( (1 + mid)/2.0 ) * mid
                if testsum <= n: # undershot
                    lo = mid + 1
                else: # overshot the sum
                    hi = mid - 1
                
            return hi
    

Log in to reply
 

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