11. Container With Most Water
通过观察面积公式,如果我们想让面积最大,就要让(right_index - left_index)
由最大值向0收敛,并且尽可能地提高Min(height[left_index], height[right_index])
的“短板”。
class Solution {
public int maxArea(int[] height) {
int left = 0;
int right = height.length - 1;
int area = 0;
while (left <= right) {
area = Math.max(area, (right - left) * Math.min(height[left], height[right]));
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return area;
}
}