LeetCode (11)Container With Most Water
2017-04-19 14:42
281 查看
(11)Container With Most Water
题目:提供一个长度为N的非负整数串a1,a2,a3,a4……ai,每一个点代表一个垂直x轴的从(i,0)到(i,ai)的线段,求两个这样的线段和x轴包围的容器可以储存的最大水量。所谓短板效应就是桶里能装的最多水量是取决于最短那个板子的,当选择容器的时候,我们不需要两层循环把所有的问题都理一遍,所以我从最外面的两个开始,从最短的那边开始向内部移一下再算一次大小,也就是说定义left和right,如果left小于right,那么left右移,否则right左移,每次的容量为std::min(height_left,height_right)*(right - left),然后保存最大的那一个就可以了。
下面是代码:
class Solution { public: int maxArea(vector<int>& height) { int maxnum = 0; int r,l,len = height.size(); r = len - 1; l = 0; while(l<r){ int minnum = std::min(height[l],height[r]); int v = (r-l)*minnum; maxnum = std::max(maxnum,v); if(height[l] == minnum){ l++; } else{ r --; } } return maxnu 92f9 m; } };
相关文章推荐
- leetcode[11]Container With Most Water
- leetcode-11 Container With Most Water
- leetcode_11题——Container With Most Water(两个指针)
- leetcode 11 -- Container With Most Water
- LeetCode 11 Container With Most Water
- [leetcode 11] Container With Most Water
- [Leetcode 11, Medium] Container With Most Water
- Container With Most Water - LeetCode 11
- LeetCode 11 - Container With Most Water
- Leetcode 11 Container With Most Water
- [LeetCode 11] Container With Most Water
- Java [leetcode 11] Container With Most Water
- Container With Most Water-Leetcode OJ Problem 11
- leetcode 11 -- Container With Most Water
- [leetcode 11] Container With Most Water
- LeetCode42/11 Trapping Rain Water/Container With Most Water/Hist area**
- LeetCode-11 Container With Most Water(容器装水最大量)
- LeetCode 11 Container With Most Water (C,C++,Java,Python)
- [Leetcode 43] 11 Container With Most Water
- [leetcode 11]Container With Most Water