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 and n is at least 2.
l = 0
r = n-1
a[l] <a[r]
l++
那边矮扔掉哪边
1 class Solution { 2 public: 3 int maxArea(vector<int>& a) { 4 const int n = a.size(); 5 int res = 0; 6 int low = 0; 7 int high = n-1; 8 while(low<=high) { 9 res = std::max((high-low)*std::min(a[low],a[high]),res); 10 if(a[low]<a[high]) { 11 low++; 12 } else { 13 high--; 14 } 15 } 16 return res; 17 } 18 };
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