码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode Container With Most Water

时间:2015-03-28 20:22:35      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

代码:

#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;
}

 

leetcode Container With Most Water

标签:

原文地址:http://www.cnblogs.com/chaiwentao/p/4374677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!