lintcode-装最多水的容器-383

给定 n 个非负整数 a1, a2, ..., an, 每个数代表了坐标中的一个点 (i, ai)。画 n 条垂直线,使得 i 垂直线的两个端点分别为(i, ai)(i, 0)。找到两条线,使得其与 x 轴共同构成一个容器,以容纳最多水。


样例

给出[1,3,2], 最大的储水面积是2.

注意

容器不可倾斜。

class Solution {
public:
    
    int maxArea(vector<int> &heights) {
        if(heights.empty())
            return 0;
        int end=heights.size()-1;
        int start=0;
        int Max;
        while(start<end){  //当start==end就退化成一个点了,所以不考虑
            int area;
            if(heights[start]<heights[end]){
                area=(end-start)*heights[start]; //计算装水的面积
                ++start;
            }else{
                area=(end-start)*heights[end];
                 --end;
            }
            Max=max(Max,area);
        }
        return Max;
    }
};


你可能感兴趣的:(lintcode-装最多水的容器-383)