-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11.container-with-most-water.cpp
More file actions
39 lines (37 loc) · 1.02 KB
/
11.container-with-most-water.cpp
File metadata and controls
39 lines (37 loc) · 1.02 KB
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
/*
* @lc app=leetcode id=11 lang=cpp
*
* [11] Container With Most Water
*/
// @lc code=start
auto speedup = [](){
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
return 0;
}();
class Solution {
public:
int maxArea(vector<int>& height) {
int len = height.size();
int left = 0, right = len - 1;
int answer = (right - left) * min(height[left], height[right]);
while(left < right) {
if(height[left] < height[right]) {
int tmp = height[left];
while(left < right && height[left] <= tmp) ++left;
answer = max(answer, (right - left) * min(height[left], height[right]));
} else {
int tmp = height[right];
while(left < right && height[right] <= tmp) --right;
answer = max(answer, (right - left) * min(height[left], height[right]));
}
}
return answer;
}
};
// Accepted
// 60/60 cases passed (150 ms)
// Your runtime beats 25.72 % of cpp submissions
// Your memory usage beats 38.72 % of cpp submissions (59.4 MB)
// @lc code=end