-
Notifications
You must be signed in to change notification settings - Fork 2
/
441. Arranging Coins.py
65 lines (57 loc) · 1.6 KB
/
441. Arranging Coins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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.
# n is 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.
class Solution:
def arrangeCoins(self, n: int) -> int:
# M1. 蛮力求和 TLE
if n <= 1:
return n
level = 1
while sum(list(range(1, level+1))) <= n:
level += 1
return level - 1
# M2. 模拟排列
level = 0
count = 0
while count + level + 1 <= n:
level += 1
count += level
return level
rows=0
while n > 0:
if n > rows:
rows += 1
else:
return rows
n = n-rows
return rows
# M3. 数学推导, 一元二次方程求解公式
return (int)((2 * n + 0.25)**0.5 - 0.5)
# M4. 数学推导, 二分查找
left, right = 0, n
while left <= right:
k = (right + left) // 2
curr = k * (k + 1) // 2
if curr == n:
return k
if n < curr:
right = k - 1
else:
left = k + 1
return right