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 - 简单的夹逼法。
2 - 初始时,选择最左和最右的两条线组成容器。
3 - 哪边比较高度较小就将哪边向中间夹逼。
class Solution {
public:
int maxArea(vector<int>& height) {
int begin = 0;
int end = height.size()-1;
int result = 0;
int area;
while(begin<end){
area = min(height[begin],height[end]) * (end-begin);
result = max(result,area);
//哪边高度较小,就将哪边向中间夹逼
if(height[begin]<height[end]){
begin++;
}else{
end--;
}
}
return result;
}
};