11. 盛最多水的容器
https://leetcode.cn/problems/container-with-most-water/description/?envType=study-plan-v2&envId=top-interview-150
我们想让水最多(长*宽),我们一开始能确定的就是宽最大为多少(最两边的柱子围成的水池),但是这并不一定是最大的,而已知宽度已经最大了(之后逐渐变小),所以我们要让长尽可能大,所以我们要让两边的柱子尽可能高,因为短板原理,所以我们要移动两边柱子中较矮的那一个,这就是典型的双指针题。
public class Main {
public int maxArea(int[] height) {
int res = 0;
int l = 0, r = height.length - 1;
while(l < r) {
res = Math.max(res, Math.min(height[l], height[r]) * (r - l));
if(height[l] < height[r]) l++;
else r--;
}
return res;
}
}