generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.cpp
37 lines (33 loc) · 1 KB
/
solution.cpp
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
#include <vector>
class Solution {
public:
int maxArea(std::vector<int>& height) {
int left = 0;
int right = height.size() - 1;
int leftHeight = height[left];
int rightHeight = height[right];
int max = std::min(leftHeight, rightHeight) * (right - left);
while (left < right) {
if (leftHeight <= rightHeight) {
while (left < right) {
++left;
if (height[left] <= leftHeight) continue;
leftHeight = height[left];
int newMax = std::min(leftHeight, rightHeight) * (right - left);
if (newMax > max) max = newMax;
if (leftHeight > rightHeight) break;
}
} else {
while (left < right) {
--right;
if (height[right] <= rightHeight) continue;
rightHeight = height[right];
int newMax = std::min(leftHeight, rightHeight) * (right - left);
if (newMax > max) max = newMax;
if (rightHeight >= leftHeight) break;
}
}
}
return max;
}
};