Container With Most Water
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.
求隔板间的面积最大。
1 class Solution { 2 public: 3 int maxArea(vector<int>& height) { 4 if(height.size()<2) return 0; 5 int result=0; 6 int left=0,right=height.size()-1; 7 while(left<right) 8 { 9 int water = min(height[left],height[right])*(right-left); 10 result = max(result,water); 11 if(height[left]<height[right]) 12 { 13 left++; 14 } 15 else 16 { 17 right--; 18 } 19 } 20 return result; 21 } 22 };