盛最多水的容器
盛最多水的容器题目的双指针思路与移动较短边的关键判断
#type / howto
#status / evergreen
#resource / algorithm
#algo / two-pointers
#topic / leetcode100
#source / leetcode
[!info] related notes
盛最多水的容器
分析
关键:能够确定下一步的移动(是左侧移动还是右侧移动)。 左边右移动 和 右边左移动 时 宽度是固定的,对面积没有影响。 所以下一步的移动就是 更短的边
function maxArea(height: number[]): number {
let left = 0, right = height.length - 1;
let ans = 0;
while (left < right) {
let area = (right - left) * (Math.min(height[left], height[right]));
ans = Math.max(area, ans);
height[left] > height[right] ? right-- : left++
}
return ans;
};