求装最多水:volume=min(高度)*(区间差)
思路:
左指针、右指针:分别指向两端的“板”;
如果左边的指针指向的高度小于右边指针指向的高度:左指针右移
【注意:如果此时左移动右指针,可能会造成值越来越小,因为区间在减小】
如果左边的指针指向的高度大于右边指针指向的高度:右指针左移
【注意:如果此时向右移动左指针,可能会造成值越来越小,因为区间在减小】
代码:
class Solution {
public:
int maxArea(vector<int>& height) {
long int maxV=0;
int leftindex=0;
int rightindex=height.size()-1;
while(leftindex<=rightindex)
{
maxV=maxV>(min(height[leftindex],height[rightindex])*(rightindex-leftindex))?maxV:(min(height[leftindex],height[rightindex])*(rightindex-leftindex));
if(height[leftindex]<=height[rightindex])
{
leftindex++;
continue;
}
else
{
rightindex--;
continue;
}
}
return maxV;
}
};