【Leetcode】【python】Container With Most Water
2017-08-10 00:58
423 查看
题目大意
利用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
总结
相关文章推荐
- 【LEETCODE】11-Container With Most Water [Python]
- 【LeetCode】【Python解读】Container with most water
- [leetcode]Container With Most Water(using Python)
- leetcode 【 Container With Most Water 】python 实现
- leetcode Container With Most Water python
- 【LeetCode with Python】 Container With Most Water
- 【leetcode】Container with most water--python实现
- **Leetcode_container-with-most-water(c++ and python version)
- Container With Most Water Leetcode Python Java
- LeetCode 11 Container With Most Water (C,C++,Java,Python)
- Leetcode -- Python --Container With Most Water
- 【LeetCode】【Python题解】Container with most water
- [leetcode]Container With Most Water @ Python
- 【LeetCode 11】Container With Most Water (Python)
- [LeetCode]题解(python):011-Container With Most Water
- leetcode:Container With Most Water 6行AC
- LeetCode :: Container With Most Water
- leetcode Container With Most Water
- Leetcode Container With Most Water
- 【LeetCode】Container With Most Water