盛水最多的容器
运行截图
/**
* 2 * @Author: LJJ
* 3 * @Date: 2023/7/9 23:02
* 4
*/
public class MaxArea {
public static int maxArea(int[] height) {
int left = 0; // 左指针
int right = height.length - 1; // 右指针
int maxArea = 0; // 最大水量
while (left < right) {
int currentArea = Math.min(height[left], height[right]) * (right - left);
maxArea = Math.max(maxArea, currentArea);
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return maxArea;
}
public static void main(String[] args) {
int[] height = {1, 8, 6, 2, 5, 4, 8, 3, 7};
int result = maxArea(height);
System.out.println(result); // 输出: 49,最大水量为 49
}
}