leetcode.com/problems/container-with-most-water/
左右两点向内缩,当左端点(le)值小于右端点(ri)值时,移动左端点,因为对于当前 le,不存在一个比 ri 靠左的端点可以得到更优的结果。
class Solution {
public:
int maxArea(vector<int>& height) {
int le = 0, ri = height.size() - 1;
int ans = 0;
while(le < ri){
if(height[le] < height[ri]){
ans = max(ans, height[le] * (ri - le));
le++;
}
else{
ans = max(ans, height[ri] * (ri - le));
ri--;
}
}
return ans;
}
};