【Leetcode】【python】Container With Most Water
来源:互联网 发布:红米1s支持4g网络吗 编辑:程序博客网 时间:2024/06/07 06:50
题目大意
利用x轴作底,给出一个数组,两个任意的竖直线段作杯壁,何时盛水最多。
解题思路
可以直接for循环i,j遍历,那复杂度就是O(n^2)
复杂度为O(n)的思想是贪心原理,先从底边最大的情况考虑,计算最大面积后,此时要将底边长度减1,只需要将杯壁较短的那一边移动一个单位距离,得到的解必定优于杯壁较长那边移动的情况。这样保证每次移动都得到的是局部最优解。每次要么左边往右移动,要么右边往左移动。
代码
class Solution(object): def maxArea(self, height): """ :type height: List[int] :rtype: int """ left = 0 right = len(height) - 1 area = 0 while left < right: area_temp = (right - left) * min(height[left], height[right]) if area_temp > area: area = area_temp if height[left] < height[right]: left += 1 else: right -= 1 return area
总结
阅读全文
0 0
- 【LeetCode with Python】 Container With Most Water
- Leetcode -- Python --Container With Most Water
- 【LeetCode】【Python题解】Container with most water
- 10.Container With Most Water Leetcode Python
- 【LEETCODE】11-Container With Most Water [Python]
- 【leetcode】Container with most water--python实现
- Container With Most Water Leetcode Python Java
- [Leetcode] Container With Most Water Python
- [leetcode: Python]11. Container With Most Water
- 【Leetcode】【python】Container With Most Water
- LeetCode: Container With Most Water
- [LeetCode]Container With Most Water
- LeetCode Container With Most Water
- LeetCode: Container With Most Water
- [Leetcode] Container With Most Water
- Leetcode: Container With Most Water
- [LeetCode] Container With Most Water
- LeetCode Container With Most Water
- Eclipse_SVN文件冲突、标记为合并
- Kubernetes基于ovs构建docker集群
- 普及认识:一种实现人工智能程序自进化的概念原理
- (10)2017.8.9-面向对象(5)
- 小知识
- 【Leetcode】【python】Container With Most Water
- VS 2017 安装 Resharper 2017的安装
- 设计模式-适配器模式-adapter-python
- java 判断
- spring mvc基础篇(五):映射处理器之BeanNameUrlHandlerMapping
- 欢迎使用CSDN-markdown编辑器
- umask命令:设置文件的默认权限掩码
- Android 利用RecyclerView 的SnapHelper 实现滚轮效果
- js操作日期