LeetCode——11. 盛最多水的容器

11. 盛最多水的容器

给定 n 个非负整数 a1a2,...,an,每个数代表坐标中的一个点 (i, ai) 。画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
注意:你不能倾斜容器,n 至少是2。


class Solution {
public:
          int maxArea(vector<int>& height) {
                   int left = 0, right = height.size() - 1;
                   int maxArea = 0;
                   while (left < right)
                   {
                             maxArea = max(maxArea, min(height[right] , height[left]) * (right - left));
                             if (height[left] < height[right])
                                      left++;
                             else
                                 right--;
                   }
                   return maxArea;
          }
};

猜你喜欢

转载自blog.csdn.net/ling_hun_pang_zi/article/details/80734806