Container With Most Water - LeetCode
来源:互联网 发布:ubuntu tmux 编辑:程序博客网 时间:2024/06/05 21:55
Container With Most Water - LeetCode
题目:
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.
分析:
这道题目一开始没注意note,结果一直以为两线与x轴成一个梯形。。然后想了半天没想出来,其实是组成个矩形,就是一个盛水槽,这个我们知道,槽子的高度取决于两侧高度的短板,这样我们可以用两个指针,l在开头,r在结尾,当l<r时,我们希望提高l,所以l++;同理r<l时,我们希望提高r,所以r--。每一次循环都判断此时面积是否是大于res的,如果大于替换掉res。结束条件自然是当r=l时。
代码:
class Solution: # @return an integer def maxArea(self, height): if not height: return 0 res = 0 l = 0 r = len(height)-1 while l<r: res = max(res,min(height[l],height[r])*(r-l)) if height[l]<height[r]: l += 1 else: r -= 1 return res
0 0
- 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
- [Leetcode] 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
- Leetcode: Container With Most Water
- 【LeetCode】Container With Most Water
- Leetcode: Container With Most Water
- LeetCode:Container With Most Water
- hdu 1024(dp*优化)
- 递归实现最大公约数:辗转相除法
- Android中的安全与访问权限控制
- 深入理解Android 中的init
- hdu 1576A/B(扩展欧几里得)
- Container With Most Water - LeetCode
- 软件项目版本号设置
- HDU 1046
- 转:模式识别 机器学习 计算机视觉 相关资料 论坛 网站 牛人...
- 蓝桥杯 填写乘法算式
- 史上最全的maven pom.xml文件教程详解
- Objective-C内存管理
- [ACM] SDUT 2604 Thrall’s Dream
- SpringWeb异常处理