DAY6:leetcode #11 Container With Most Water
来源:互联网 发布:电脑txt小说编辑软件 编辑:程序博客网 时间:2024/04/30 12:03
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container.
一开始用了两层循环遍历的思路,超时。看到了这样的思路:采用两个指针l和r,初始化分别指向数组的两端,然后在向中间移动找到最大容量。如果l指向的数字小,则l需要右移才有可能获得更大容量,因为此时如果左移r,得到的容量肯定比左移r之前的容量小(高度已经被较小的l限制住了)。如果r指向的数字小,则需要左移r。这样,当l和r相遇的时候,最大的容量就是我们需要的。
class Solution(object): def maxArea(self, height): """ :type height: List[int] :rtype: int """ max_v = 0 i = 0 j = len(height) - 1 while True: if max_v < (j-i)*min(height[i],height[j]): max_v = (j-i)*min(height[i],height[j]) if height[i] < height[j]: i += 1 else: j -= 1 if i == j: break return max_v
0 0
- DAY6: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
- leetcode-11Container With Most Water
- linux中如何让命令的别名重启后还生效
- myeclipse10下启动web项目报空指针的错误解决办法
- 倒计时功能CountDownTimer PK Handler
- PHP 魔术函数、魔术常量
- oracle启动过程
- DAY6:leetcode #11 Container With Most Water
- Delphi 编写DLL动态链接库文件的知识和样例
- Eclipse一直building workspace问题解决
- 十一.Shell编程之运算符
- 信管 操作系统
- SQL---用一张表中的一列更新另一张表中的一列
- 异常处理Manual close is not allowed over a Spring managed SqlSession
- HttpClient模拟get,post请求并发送请求参数(json等)
- vs2008下win32 dll封装笔记