-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path011. Container With Most Water.cpp
34 lines (33 loc) · 1.07 KB
/
011. Container With Most Water.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
// My BF with optimization, O(n^2)
class Solution {
public:
int maxArea(vector<int>& height) {
int maxArea = 0, minLeft = 0, minRight = 0;
for(int i = height.size() - 1; i >= 0; i--){
if(height[i] < minRight) continue;
minLeft = 0;
for(int j = 0; j < i; j++){
if(height[j] < minLeft) continue;
maxArea = max(maxArea, min(height[i], height[j]) * (i - j));
minLeft = max(minLeft, height[j]);
}
minRight = max(minRight, height[i]);
}
return maxArea;
}
};
// Brilliant O(n) from Stefan: https://discuss.leetcode.com/topic/16754/simple-and-fast-c-c-with-explanation
class Solution {
public:
int maxArea(vector<int>& height) {
int water = 0;
int i = 0, j = height.size() - 1;
while (i < j) {
int h = min(height[i], height[j]);
water = max(water, (j - i) * h);
while (height[i] <= h && i < j) i++;
while (height[j] <= h && i < j) j--;
}
return water;
}
};