//time complexity: O(n).
//the max area between twp lines should be decided by the min line
public int maxArea(int[] height) {
// Start typing your Java solution below
// DO NOT write main() function
if(height.length == 0) return 0;
int result = 0;
int max = 0;
int start = 0;
int end = height.length - 1;
while(start < end) {
if(height[start] < height[end]) {
max = height[start] * (end - start);
start++;
}else {
max = height[end] * (end - start);
end--;
}
result = Math.max(result, max);
}
return result;
}
Container With Most Water
最新推荐文章于 2019-11-06 10:22:53 发布