LeetCode 11.盛最多水的容器(测试+代码)
package LeetCode;
class Solution {
public static void main(String[] args) {
int []array={1,8,6,2,5,4,8,3,7};
int result= maxArea(array);
System.out.println(result);
}
public static int maxArea(int[] height) {
//if(height==null||height.length<2) return 0;
int max=0;
int left=0; //straing index
int right=height.length-1; //ending index
while (left < right) {
int h = Math.min(height[left], height[right]);
max = Math.max(max, (right - left) * h);
if (height[left] < height[right]) {
left++;
} else if (height[left] > height[right]) {
right--;
} else {
left++;
right--;
}
}
return max;
}
}