应该使用什么类型的双指针解决此题?
- 容器的容积和最短边长和底边长有关,要求最大的容积,可以用贪心的思想,即让底边长尽可能大,以及左右边长尽可能大。
- 要使左右边长尽可能大,则应该使用对撞指针,即初始时左右指针指向首尾,要使最短边长尽可能大,则左右指针应该是边长小的一边移动,因为贪心思想下较长的一边比较短的一边更可能出现更大容积,移动过程更新最大容积。
参考
https://blog.nowcoder.net/n/48d8b18532324e60817a9a8db21b376d?f=comment
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param height int整型一维数组
* @return int整型
*/
export function maxArea(height: number[]): number {
// write code here
let maxVolume = 0;
let left = 0;
let right = height.length-1;
while (left < right) {
maxVolume = Math.max(maxVolume, Math.min(height[left], height[right])*(right-left));
height[left] <= height[right] ? ++left : --right;
}
return maxVolume;
}



京公网安备 11010502036488号