Fast Python solution O(log n) [55ms]


  • 0

    Binary (bit by bit) search solution in Python:

    class Solution(object):
        def arrangeCoins(self, n):
            """
            :type n: int
            :rtype: int
    
            Time: O(log n)
            Space: O(1)
    
            """
            s = 0
            b = 0x8000
            while b:
                s ^= b
                m = s * (s + 1) >> 1
                if m > n:
                    s ^= b
                b >>= 1
            
            return s
    

Log in to reply
 

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