LeetCode11
连接 https://leetcode-cn.com/problems/container-with-most-water/
一、暴力解法
class Solution {
public int maxArea(int[] height) {
// int i=0;
// int j = height.length-1;
int min;
int max=-1;
for(int i=0;i<height.length;i++){
for(int j=i;j<height.length;j++){
min = height[i]>height[j]?height[j]:height[i];
max = min*(j-i)>max?min*(j-i):max;
}
}
return max;
}
}
双指针,类似贪心,每次选择可能将面积变大的方法,也就是将边界值变大
public class Solution {
public int maxArea(int[] height) {
int l = 0, r = height.length - 1;
int ans = 0;
while (l < r) {
int area = Math.min(height[l], height[r]) * (r - l);
ans = Math.max(ans, area);
if (height[l] <= height[r]) {
++l;
}
else {
--r;
}
}
return ans;
}
}