题目
题解
- 从两端开始遍历
容纳的水量=两个指针指向的数字中较小值∗指针之间的距离
短板向内缩进,如果长板向内缩进只会不变或者减小 - 相对于暴力枚举,贪心算法指,所有的消去状态都不会导致丢失面积最大值 。
代码
class Solution {
public://最高与次高距离
int maxArea(vector<int>& height) {
int l=0,r=height.size()-1;
int res=0;
while(l<r){
int sqr=min(height[l],height[r])*(r-l);
res=max(res,sqr);
if(height[l]<height[r])
l++;
else
r--;
}
return res;
}
};