题目描述

给定n个非负整数a1,a2,…,an,其中每个数字表示坐标(i, ai)处的一个点。以(i,ai)和(i,0)(i=1,2,3...n)为端点画出n条直线。你可以从中选择两条线与x轴一起构成一个容器,最大的容器能装多少水?
注意:你不能倾斜容器
leetcode 138:container-with-most-water
例如:
输入 [1,8,6,2,5,4,8,3,7]
输出: 49
题目分析:
容器中水的容量等于容器左壁与右壁之间的距离与左壁右壁之间高度最小的值之间的乘积。这题最关键的是两点:一是两边往中间找,二是每次放弃最短的板。
代码如下:
1 int maxArea(vector<int>& height) {
2         int left,right,Max =-9999;
3         for(left = 0,right = height.size() - 1;left < right;)
4         {
5             Max = max(Max,(right - left)*min(height[left],height[right]));
6             height[left]<height[right]?left++:right--;
7         }
8         return Max;
9     }

 

相关文章:

  • 2021-09-01
  • 2021-06-10
  • 2021-10-23
  • 2021-12-26
猜你喜欢
  • 2022-02-28
  • 2021-08-03
  • 2021-09-06
  • 2021-11-21
  • 2022-01-06
  • 2022-03-03
  • 2021-08-22
相关资源
相似解决方案