LeetCode11. Container With Most Water
求水池蓄水最大面积
思路:维护一个变量max_pool 记录最大,双指针左右,哪边小 乘以r-l与max_pool比较,更新max_pool 哪边小移动哪边
beat 98.3
class Solution {
public:
int maxArea(vector<int>& height) {
int l=0;
int max_pool=0;
int r=height.size()-1;
while(l<height.size()&&r>0){
if(height[l]<height[r]){
max_pool=max(max_pool,height[l]*(r-l));
l++;
}
else{
max_pool=max(max_pool,height[r]*(r-l));
r--;
}
}
return max_pool;
}
};