题目描述:
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.
思路:
本题可以抽象为一个条形图,要求计算出两条直线能围成的最大面积(以短的直线为高,以两直线的距离为长)。
- 设置两个变量 left 和 right , left 指示最左边的直线, right 指示最右边的直线。
- 每次计算出当前 left 和 right 围成的面积,并通过 area 保存最大面积;
- 当 left 比 right 短, left 向右移动(left++),当 right 比 left 短, right 向左移动(right- -),以计算是否有更大的面积。
代码实现:
class Solution {
public:
int maxArea(vector<int>& height) {
int left = 0;
int right = height.size()-1;
int area = 0;
while(left<right){
area = max(area,min(height[left],height[right])*(right-left));
if(height[left]<height[right]){
left++;
}else{
right--;
}
}
return area;
}
};