11. 盛最多水的容器
双指针法:
#Python
class Solution:
def maxArea(self, height: List[int]) -> int:
left, right = 0, len(height) - 1
ans = []
while left < right:
cur = min(height[left], height[right]) * (right - left)
ans.append(cur)
if height[left] < height[right]:
left += 1
else:
right -= 1
return max(ans)
//C++
class Solution {
public:
int maxArea(vector<int>& height) {
int l = 0, r = height.size() - 1;
int ans = 0;
while (l < r) {
int area = min(height[l], height[r]) * (r - l);
ans = max(ans, area);
if (height[l] <= height[r]) {
++l;
}
else {
--r;
}
}
return ans;
}
};