leetcode Container With Most Water

代码:

#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)