-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathmaximum-height-of-a-triangle.py
More file actions
43 lines (38 loc) · 1000 Bytes
/
maximum-height-of-a-triangle.py
File metadata and controls
43 lines (38 loc) · 1000 Bytes
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
# Time: O(logn)
# Space: O(1)
# math
class Solution(object):
def maxHeightOfTriangle(self, red, blue):
"""
:type red: int
:type blue: int
:rtype: int
"""
def f(x, y):
# odd level:
# (1+h)*((1+h)//2)//2 <= x
# => h <= int(2*x**0.5)-1
# even level:
# (2+h)*(h//2)//2 <= y
# => h <= int((4*y+1)**0.5)-1
a, b = int(2*x**0.5)-1, int((4*y+1)**0.5)-1
return min(a, b)+int(a != b)
return max(f(red, blue), f(blue, red))
# Time: O(sqrt(n))
# Space: O(1)
# simulation
class Solution2(object):
def maxHeightOfTriangle(self, red, blue):
"""
:type red: int
:type blue: int
:rtype: int
"""
def f(x, y):
h = 0
while x >= h+1:
h += 1
x -= h
x, y = y, x
return h
return max(f(red, blue), f(blue, red))