-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.盛最多水的容器.cpp
33 lines (32 loc) · 973 Bytes
/
11.盛最多水的容器.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
/*
* @lc app=leetcode.cn id=11 lang=cpp
*
* [11] 盛最多水的容器
*/
class Solution {
public:
int maxArea(vector<int>& height) {
int maxWater = 0;
int left = 0, right = height.size()-1;
while(left < right){
maxWater = (maxWater < (right-left)*std::min(height[left], height[right]))?(right-left)*std::min(height[left], height[right]):maxWater;
if(height[left] < height[right]){
for(int i = 1; i <= right-left; i++){
if(height[left+i] >= height[left]){
left = left+i;
break;
}
}
}
else{
for(int i = 1; i <= right-left; i++){
if(height[right-i] >= height[right]){
right = right-i;
break;
}
}
}
}
return maxWater;
}
};