LeetCode (11)Container With Most Water
来源:互联网 发布:linux下安装python 编辑:程序博客网 时间:2024/06/07 07:06
(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 maxnum; }};
0 0
- 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] 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
- Container With Most Water - LeetCode 11
- [Leetcode 11, Medium] Container With Most Water
- LeetCode 11 - Container With Most Water
- leetcode 11 -- Container With Most Water
- #LeetCode OJ #11 Container With Most Water
- Leetcode #11 Container With Most Water
- C#图片处理示例(裁剪,缩放,清晰度,水印)
- Java入门需要从哪里入手学习
- 【寒江雪】测试优先级策略
- linux上导入txt文件数据到mysql自己写的例子
- single number问题
- LeetCode (11)Container With Most Water
- SecureCRT下的串口不能输入
- B树,B+树,B-树,B*树
- zeppelin源码分析(3)——主要的class分析(中)
- NSLog重定向
- Windows系统安装Pcapy模块
- Ajax的跨域请求问题
- AlertDialog的用法
- jdbc 基础