11. 盛最多水的容器
js
/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function (height) {
// 时间复杂度 O(n)
// 空间复杂度 O(1)
let len = height.length;
let left = 0,
right = len - 1;
let res = 0;
while (left < right) {
let area = Math.min(height[left], height[right]) * (right - left);
res = Math.max(res, area);
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return res;
};
console.log(maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7]));
py
class Solution:
def maxArea(self,height:List[int])->int:
ans = 0
left = 0
right = len(height)-1
while(left<right):
area = (right-left)*min(height[left],height[right])
ans = max(area,ans)
if(height[left]<height[right]):
left+=1
else:
right -=1
return ans