地址:https://leetcode-cn.com/problems/container-with-most-water/
方法一:暴力解法
枚举所有的容器的两个内壁的下标,计算水的容量,选出最大值。
Java 代码:
public class Solution {
// 暴力解法,时间复杂度太高,我们应该使用指针对撞的方法
public int maxArea(int[] height) {
int len = height.length;
if (len < 2) {
return 0;
}
int res = 0;
for (int i = 0; i < len - 1; i++) {
for (int j = i + 1; j < len; j++) {
res = Math.max(res, Math.min(height[i], height[j]) * (j - i));
}
}
return res;
}
public static void main(String[] args) {
int[] height = {1, 8, 6, 2, 5, 4, 8, 3, 7};
Solution solution = new Solution();
int res = solution.maxArea(height);
System.out.println(res);
}
}
方法二:双指针
Java 代码:
public class Solution {
public int maxArea(int[] height) {
int len = height.length;
if (len < 2) {
return 0;
}
int left = 0;
int right = len - 1;
int res = 0;
while (left < right) {
int minHeight = Math.min(height[left], height[right]);
res = Math.max(res, minHeight * (right - left));
if (height[left] == minHeight) {
left++;
} else {
right--;
}
}
return res;
}
}
使用双指针的原因是根据这个问题的特点,存水的高度取决于两边较短的那个内壁的高度。
使用指针对撞的方式不会错过最优解。
经验:双指针、滑动窗口的问题,一般先从暴力枚举开始思考,然后更改枚举的顺序,以达到剪枝加快计算的效果。
可以参考 盛最多水的容器(双指针法,易懂解析,图解),写题解的同学 @Krahets 非常用心,且很热心解答朋友的疑问。我写题解的结构也参考了他的思路,希望对大家有帮助。