题目:https://leetcode-cn.com/problems/container-with-most-water/
思路:双指针,一头一尾,谁较小谁先往中间移动,不断更新此时的最大面积
代码:
var maxArea = function(height) {
let i = 0, j = height.length - 1, maxArea = 0
while (i < j) {
maxArea = Math.max(maxArea, (j - i) * Math.min(height[i], height[j]))
if (height[i] <= height[j]) {
i ++
} else {
j --
}
}
return maxArea
};
结果: