贪心算法:设置左右两个指针,选取高度小的那个为上限,计算当前左右柱子围起来的水量和max哪个大即可。
public class Solution {
public int maxArea (int[] height) {
int left =0,right = height.length-1;
int max = 0;
while(left<right){
if(height[left]<height[right]){
max = Math.max(max,(height[left]*(right-left)));
left++;
}
else {
max = Math.max(max,(height[right]*(right-left)));
right--;
}
}
return max;
}
}