public class Solution {
public int maxArea(int[] height) {
if (height.length < 2)
return 0;
int area = 0, h1, h2;
for (int i = 0, j = height.length - 1; i < j;) {
h1 = height[i];
h2 = height[j];
area = Math.max(area, Math.abs(i - j) * Math.min(h1, h2));
if (h1 < h2)
i++;
else
j--;
}
return area;
}
}
------------------
HINT:
解题手法:
高度小的这边往高度大的那边挪动,保留高度大的;
因为如果高度大往高度小的,面积只会越来越小。
==========
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container.