"""
Two Pointer Approach
The intuition behind this approach is that the area formed between the lines will always be limited by the height of the shorter line.
Further, the farther the lines, the more will be the area obtained.
We take two pointers, one at the beginning and one at the end of the array constituting the length of the lines.
Futher, we maintain a variable #maxarea# maxarea to store the maximum area obtained till now.
At every step, we find out the area formed between them, update #maxarea# maxarea
and move the pointer pointing to the shorter line towards the other end by one step.
"""
class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
if not height:
return 0;
start = 0;
end = len(height) - 1;
maxarea = 0;
while(start < end):
maxarea = max(maxarea, (end - start) * min(height[start],height[end]));
if height[start] < height [end]:
start += 1;
else:
end -= 1;
return maxarea;
public class Solution {
public int maxArea(int[] height) {
if (height == null || height.length < 2) return 0;
int i = 0;
int j = height.length - 1;
int res = 0;
while (i < j) {
res = Math.max(res, Math.min(height[i], height[j]) * (j - i));
if (height[i] < height[j]) {
i++;
} else {
j--;
}
}
return res;
}
}
public class Solution {
public int maxArea(int[] height) {
int area;
int i = 0;
int j = height.length - 1;
int low = 0;
int max = 0;
while (i < j) {
if (height[i] <= low) {
i++;
continue;
}
if (height[j] <= low) {
j--;
continue;
}
if (height[i] > height[j]) {
low = height[j];
area = low * (j - i);
j--;
if (area > max) {
max = area;
}
}
else {
low = height[i];
area = low * (j - i);
i++;
if (area > max) {
max = area;
}
}
}
return max;
}
}