美文网首页
[LeetCode](week1) 11.Container W

[LeetCode](week1) 11.Container W

作者: jeff98 | 来源:发表于2018-09-09 00:51 被阅读0次

题目描述

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 and n is at least 2.

题解

class Solution
{
  public:
    int maxArea(vector<int> &height)
    {
        int size = height.size();
        int max = 0;
        for (int i = 0; i < (size - 1); ++i)
        {
            for (int e = i + 1; e < size; ++e)
            {
                int temp = (height[i] > height[e]) ? height[e] : height[i];
                // cout << "\t" << i << "\t" << e << "\t" << temp << "\t" << (e - i) * temp << endl;
                if ((e - i) * temp > max)
                    max = (e - i) * temp;
            }
        }
        return max;
    }
};

相关文章

网友评论

      本文标题:[LeetCode](week1) 11.Container W

      本文链接:https://www.haomeiwen.com/subject/pkxrgftx.html