双指针
盛最多水的容器
来源: https://leetcode.cn/problems/container-with-most-water/description/
输入:[1,8,6,2,5,4,8,3,7]
输出:49
解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。
//来源:https://leetcode.cn/problems/container-with-most-water/solutions/207215/sheng-zui-duo-shui-de-rong-qi-by-leetcode-solution/
// 总是移动数字较小的那个指针
// [1, 8, 6, 2, 5, 4, 8, 3, 7]
// ^ ^
function maxArea(height) {
// 定义两个指针
let l = 0,
r = height.length - 1;
// 最大值
let ans = 0;
while (l < r) {
let area = Math.min(height[l], height[r]) * (r - l);
ans = Math.max(ans,area);
if(height[l] <= height[r]) {
++l
}else {
--r
}
}
return ans
}
Powered by Waline v2.13.0