leetcode个人题解——#11 Container with most water

时间:2023-03-09 04:39:47
leetcode个人题解——#11 Container with most water
class Solution {
public:
int maxArea(vector<int>& height) {
int max = ;
int l = ;
int r = height.size()-;
while(l < r)
{
int h = min(height[l], height[r]);
int area = h * (r-l);
if (area > max)
max = area;
while (height[l] <= h && l < r) l++;
while (height[r] <= h && l < r) r--;
}
return max;
}
};

思路:先计算最宽的面积,当中间每一个容器高度小于当前两端最低点时,肯定是不符合条件的,因为宽也减少了,略过即可。