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.
class Solution:
def maxArea(self, height):
start = 0;
end = len(height) - 1;
ans = -1;
maxx = -1;
while start <= end:
maxx = (end-start)*min(height[start],height[end]);
if ans < maxx:
ans = maxx;
if height[start] <= height[end]:
start += 1;
else:
end -= 1;
return ans;
两个指针i, j分别从前后向中间移动,两个指针分别表示容器的左右边界会不会有这种可能:当前的i 和 某个k (k > j)是最大容量, 这也是不可能的,因为按照我们的移动规则,既然右指针从k 移动到了j,说明i 的左边一定存在一个边界 m,使m > k,那么[m, k]的容量肯定大于[i, k],所以[i,k]不可能是最大容量