You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.
Given n, find the total number of full staircase rows that can be formed.
nis a non-negative integer and fits within the range of a 32-bit signed integer.
Example 1:
n = 5
The coins can form the following rows:
¤
¤ ¤
¤ ¤
Because the 3rd row is incomplete, we return 2.
Example 2:
n = 8
The coins can form the following rows:
¤
¤ ¤
¤ ¤ ¤
¤ ¤
Because the 4th row is incomplete, we return 3.
题目大意:
你有n枚硬币,想要组成一个阶梯形状,其中第k行放置k枚硬币。
给定n,计算可以形成的满阶梯的最大行数。
n是非负整数,并且在32位带符号整数范围之内。
解题思路:
解法I 解一元二次方程(初等数学):
The problem is basically asking the maximum length of consecutive number that has the running sum lesser or equal to `n`. In other word, find `x` that satisfy the following condition:
class Solution(object):
def arrangeCoins(self, n):
"""
:type n: int
:rtype: int
"""
return int(math.sqrt(2 * n + 0.25) - 0.5)
class Solution(object):
def arrangeCoins(self, n):
"""
:type n: int
:rtype: int
"""
#return int(math.sqrt(2 * n + 0.25) - 0.5)
l, r = 1, n
while l <= r:
m = (l + r) / 2
if m * (m + 1) / 2 > n:
r = m - 1
elif m * (m + 1) / 2 == n:
return m
else:
l = m + 1
return l-1
class Solution(object):
def arrangeCoins(self, n):
"""
:type n: int
:rtype: int
"""
start, end = 1, n
while start+1 < end:
mid = (start+end)/2
if mid*(mid+1)/2 < n:
start = mid
else:
end = mid
if end*(end+1)/2<=n:
return end
return start