程序员

容器最大容水量

2018-11-27  本文已影响0人  静水流深ylyang

版权声明:本文为博主原创文章,转载请注明出处。
个人博客地址:https://yangyuanlin.club
欢迎来踩~~~~


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.

#include<iostream>
#include<vector>
using namespace std;
int maxArea(vector<int> &height)
{
    int Max = 0,left = 0,right = height.size() - 1;
    while(left < right)
    {
        Max = max(Max, (right-left) * min(height[left], height[right]));
        height[left] < height[right] ? left++ : right--;
    }
    return Max;
}
int main()
{
    vector<int> height;
    for(int i = 0; i < 10; i++)
    {
        int h;
        cin >> h;
        height.push_back(h);
    }
    cout << maxArea(height) << endl;
    return 0;
}

运行结果:通过


版权声明:本文为博主原创文章,转载请注明出处。
个人博客地址:https://yangyuanlin.club
欢迎来踩~~~~


上一篇 下一篇

猜你喜欢

热点阅读