11. Container With Most Water(装最多的水 双指针)

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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 and n is at least 2.

 

 

l = 0 

r = n-1

a[l] <a[r]

l++

那边矮扔掉哪边

11. Container With Most Water(装最多的水 双指针)

 

 1 class Solution:
 2     def maxArea(self, a):
 3         """
 4         :type height: List[int]
 5         :rtype: int
 6         """
 7         maxArea = 0
 8         l = 0
 9         r = len(a) - 1
10         while(l<r):
11             maxArea = max(maxArea,min(a[l],a[r])*(r-l))
12             if(a[l]<a[r]):
13                 l+=1
14             else :
15                 r-=1
16         return maxArea
17