从两端开始搜索,每次只移动较小的坐标。证明略,因为不是很懂。
class Solution {
public int maxArea(int[] height) {
if(height == null || height.length == 0) return 0;
int l = 0;
int r = height.length-1;
int maxRes = 0;
while (l<=r) {
//maxRes = Math.max(maxRes,(r-l)*Math.min(height[l],height[r]));
if(height[l]<height[r]) {
maxRes = Math.max(maxRes,(r-l)*height[l]);
l++;
}else {
maxRes = Math.max(maxRes,(r-l)*height[r]);
r--;
}
}
return maxRes;
}
}