题意:(i,ai)二维坐标上的一组数组。给出这么一个数组,计算出任意两个数据能够组成的最大的面积。
思路:短板原理。维护两个指针,一前一后,计算面积。
代码:
package com.ContainerWithMostWater;
public class CalWater {
public int calWater(int[] heights) {
int low = 0 ;
int high = heights.length -1;
int area = 0;
int maxarea = 0;
while (low < high) {
area = Math.min(heights[high], heights[low]) * (high - low);
maxarea = Math.max(area, maxarea);
if(heights[low] < heights[high]) low++;
else
high --;
}
return maxarea;
}
public static void main(String[] args) {
int[] heights = {2,4,1,3,5};
System.out.println(new CalWater().calWater(heights));
}
}