-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy patharranging-coins.py
53 lines (47 loc) · 1.13 KB
/
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
# V1
class Solution(object):
def arrangeCoins(self, n):
i = 1
if n == 1:
return 1
while (i*(i+1))/2 < n:
i = i + 1
if (i*(i+1))/2 == n:
return i
return i - 1
# V1'
# The core of this problem is solving this equation : k^2 + k > 2n
# which has 2 roots : k = (- 1 +- (1+8*n)^(1/2))/2
# so we take the "positve root" -> k = (- 1 + (1+8*n)^(1/2))/2
# k = (math.sqrt((1+8*n)/2) - 1)/2
import math
class Solution(object):
def arrangeCoins(self, n):
return int((math.sqrt(1+8*n) - 1)/2)
# V2
# Time: O(logn)
# Space: O(1)
import math
class Solution(object):
def arrangeCoins(self, n):
"""
:type n: int
:rtype: int
"""
return int((math.sqrt(8*n+1)-1) / 2) # sqrt is O(logn) time.
# Time: O(logn)
# Space: O(1)
class Solution2(object):
def arrangeCoins(self, n):
"""
:type n: int
:rtype: int
"""
left, right = 1, n
while left <= right:
mid = left + (right - left) / 2
if 2 * n < mid * (mid+1):
right = mid - 1
else:
left = mid + 1
return left - 1