leetcode 441. arrangement coins (Python)

1. Title Description

You have a total of n coins, you need to put them into the shape of a ladder, the k-th row must have exactly k coins.

Given a number n, to find the full number of rows can be formed in a stepped line.

n is a nonnegative integer, and in the range of 32-bit signed integer in.

Example 1:

n = 5

Coins can be arranged in the following lines:
¤
¤ ¤
¤ ¤

Because the third row is not complete, so the return 2.
Example 2:

n = 8

Coins can be arranged in the following lines:
¤
¤ ¤
¤ ¤ ¤
¤ ¤

Because the fourth row is not complete, it returns 3.

2. Code

class Solution:
    def arrangeCoins(self, n: int) -> int:
        if n==0:
            return 0
        count = 0
        for i in range(n):
            row = i
            count = count + row + 1
            if count > n:
                return i
            if count == n:
                return i+1

 

Guess you like

Origin www.cnblogs.com/xiaotongtt/p/11415469.html