Python easy solution


  • 0
    A

    To solve this question, we just need to find the greatest sum of consecutive integers starting from 1 which is less than or equal to n. let k be the number of rows in the answer, so the sum of 1+2+3+...+k=k(k+1)/2, which is the closet value less than or equal to n.
    So we know that k(k+1)/2 <= n, as a result k(k+1)<= 2n and consequently, k * k < 2 * n.
    This gives us an easy stat point, lets start k with floor(sqrt(n)) and drop it down by one, until k(k+1)/2< 2
    n.
    So the answer is:

    def arrangeCoins(self, n):
            """
            :type n: int
            :rtype: int
            """
            k = math.floor(math.sqrt(2*n))
            print(k)
            while ((k*(k+1)) > 2*n ):
                k=k-1
            return(k)
    

Log in to reply
 

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