<11>——Container With Most Water
来源:互联网 发布:mac磁盘管理工具 编辑:程序博客网 时间:2024/06/06 02:43
11、Container With Most Water
装最多水的容器
给定 n 个非负整数 a1, a2, ..., an, 每个数代表了坐标中的一个点 (i, ai)
。画 n 条垂直线,使得 i 垂直线的两个端点分别为(i, ai)
和(i, 0)
。找到两条线,使得其与 x 轴共同构成一个容器,以容纳最多水。
注意事项
容器不可倾斜。
样例给出[1,3,2]
, 最大的储水面积是2
.
分析:
1.蓄水情况下,高应该为两条边中较小的一条;2.我们取最左右两条边为起始边,这样宽就能达到最大,往后的边便必须都高于起始边的高度才可能达到更大的面积。
代码:class Solution {public: int maxArea(vector<int>& height) { int result=0; int i=0,j=height.size()-1; while(i<j) { int h=min(height[i],height[j]); result=max(result,(j-i)*h); while(height[i]<=h&&i<j)i++; while(height[j]<=h&&i<j)j--; } return result; }};
给定 n 个非负整数 a1, a2, ..., an, 每个数代表了坐标中的一个点 (i, ai)
。画 n 条垂直线,使得 i 垂直线的两个端点分别为(i, ai)
和(i, 0)
。找到两条线,使得其与 x 轴共同构成一个容器,以容纳最多水。
注意事项
容器不可倾斜。
给出[1,3,2]
, 最大的储水面积是2
.
class Solution {public: int maxArea(vector<int>& height) { int result=0; int i=0,j=height.size()-1; while(i<j) { int h=min(height[i],height[j]); result=max(result,(j-i)*h); while(height[i]<=h&&i<j)i++; while(height[j]<=h&&i<j)j--; } return result; }};
阅读全文
0 0
- <11>——Container With Most Water
- LeetCode——Container With Most Water
- leetcode——Container With Most Water
- Container With Most Water——LeetCode
- leetcode——Container With Most Water
- LeetCode——Container With Most Water
- Container With Most Water —— Leetcode
- leetcode——Container With Most Water
- LeetCode——Container With Most Water
- LeetCode——Container With Most Water
- LeetCode11——Container With Most Water
- Algorithms—11.Container With Most Water
- leetcode—Container With Most Water
- leetcode——11——Container With Most Water
- #11 LeetCode——Container With Most Water
- leetcode第11题——**Container With Most Water
- 【leetcode】Array——Container With Most Water(11)
- LeetCode(11)——Container With Most Water
- Objective-C中获取视频指定帧的方法
- 欢迎使用CSDN-markdown编辑器
- Android消息提示Toast的使用总结
- 真正的软件测试实习2
- 没有动力时可以一看
- <11>——Container With Most Water
- 嵌入式系统开发-5.2
- R
- Minimum Time Difference问题及解法
- 手撕代码(二):归并排序
- 例题1.25 侏罗纪 Jurassic Remains UVALive
- python rest_framework
- HDU
- java中的Error和Exception层级关系示意图