leetcode.array--11. Container With Most Water
来源:互联网 发布:日本电脑软件 编辑:程序博客网 时间:2024/05/22 00:24
题目:11. Container With Most Water
问题描述:https://leetcode.com/problems/container-with-most-water/description/
最容易的n方超时,于是想想怎么降低其实对于横坐标为i和j的两条线段(i初始值为0,j初始值为len(height)-1),其最大容积vmax=min(height[i],height[j])*(j-i)。考虑如下情况:当height[i]<height[j]时,此时容器高度为height[i],下一步如何做?i++还是j--?假设做j--取线段j的左边一条线段,如果此时的height[j]仍大于height[i],由于底变短了高不变因此容积减小;如果此时的height[j]小于height[i],会造成容器的高减小变为height[j]并且底边减小,因此容积减小。于是选择i++,即取线段i的右边一条,此时由于底减少而高度有可能增加会导致容积可能增大。因此O(n)可解决了。
代码:
class Solution(object): def maxArea(self, height): """ :type height: List[int] :rtype: int """ res=0;i=0;j=len(height)-1 while i<j: res=max(res,min(height[i],height[j])*(j-i)) if height[i]<height[j]: i+=1 else: j-=1 return res
阅读全文
1 0
- leetcode.array--11. Container With Most Water
- Leetcode/array/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
- 11. Container With Most Water LeetCode
- 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
- Leetcode 11. Container With Most Water
- leetcode 11.Container With Most Water
- leetcode 11. Container With Most Water
- 设计模式
- pgsql 物理备份
- 技术文章 | CVE-2017-12615/CVE-2017-12616:Tomcat信息泄漏和远程代码执行漏洞分析报告
- Android Butterknife 8.4.0 使用方法总结
- 让IT更精简,让项目更敏捷-如何成功运用敏捷方法进行项目管理与实施
- leetcode.array--11. Container With Most Water
- java集合详解
- 大数据时代下的隐私保护
- 交换两个变量的值
- Curling 2.0-OJ
- 大Demo
- Linux nohup 命令
- JVM调优总结(六)-分代垃圾回收详述2
- opencv学习中——MFC的小问题