题意:(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));
}
}
本文介绍了一种计算二维坐标上数组能形成的最大盛水量的算法。通过维护两个指针从两端向中间移动的方式,利用短板原理高效求解。示例代码展示了如何实现这一算法。
7453

被折叠的 条评论
为什么被折叠?



