-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathmaximum-height-of-a-triangle.cpp
More file actions
40 lines (36 loc) · 991 Bytes
/
maximum-height-of-a-triangle.cpp
File metadata and controls
40 lines (36 loc) · 991 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
// Time: O(logn)
// Space: O(1)
// math
class Solution {
public:
int maxHeightOfTriangle(int red, int blue) {
const auto& f = [](int x, int 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
const int a = static_cast<int>(2 * sqrt(x)) - 1;
const int b = static_cast<int>(sqrt(4 * y + 1)) - 1;
return min(a, b) + (a != b ? 1 : 0);
};
return max(f(red, blue), f(blue, red));
}
};
// Time: O(sqrt(n))
// Space: O(1)
// simulation
class Solution2 {
public:
int maxHeightOfTriangle(int red, int blue) {
const auto& f = [](int x, int y) {
int h = 0;
for (; x >= h + 1; swap(x, y)) {
x -= ++h;
}
return h;
};
return max(f(red, blue), f(blue, red));
}
};