红桃J

用心写好每行完美的代码,远比写一堆更有价值

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

代码:

#include<iostream>
#include<vector>

using namespace std;

int maxArea(vector<int> &height)
{
    int L = height.size();
    int i = 0;
    int j = L - 1;
    int max = 0;
    int left = height[i];
    int right = height[j];
    while (i < j)
    {
        int area;
        if (height[i] < height[j])
        {
            area = height[i] * (j - i);
            while (height[++i] <= left)
                ;
            left = i;
        }
        else
        {
            area = height[j] * (j - i);
            while (height[--j] <= right)
                ;
            right = height[j];
        }
        if (area > max)
            max = area;
    }
    return max;
}

int main()
{
    vector<int> height = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
    cout << maxArea(height) << endl;
}

 

posted on 2015-03-28 19:33  红桃J  阅读(138)  评论(0编辑  收藏  举报