题目描述
https://leetcode.cn/problems/container-with-most-water/
解题思路
参照:https://leetcode.cn/problems/container-with-most-water/solution/container-with-most-water-shuang-zhi-zhen-fa-yi-do/
本题思路的核心就是寻找两个面积最大的线。
暴力在10^5,会超时,所以使用双指针。
左右两个指针,哪边小哪边就移动,因为面积根据高度小的线来决定,所以移动小的线在有可能面积增大。
public int maxArea(int[] height) {
int i = 0, j = height.length - 1, res = 0;
while (i < j){
// 哪边小,就移动哪边的边界
res = height[i] >= height[j] ?
Math.max((j - i) * height[j--], res) :
Math.max((j - i) * height[i++], res);
}
return res;
}