1 Star 0 Fork 0

wd6/LeetCode-1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
container-with-most-water.py 862 Bytes
一键复制 编辑 原始数据 按行查看 历史
Allen Liu 提交于 11年前 . update
# Time: O(n)
# Space: O(1)
#
# 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.
#
class Solution:
# @return an integer
def maxArea(self, height):
max_area, i, j = 0, 0, len(height) - 1
while i < j:
max_area = max(max_area, min(height[i], height[j]) * (j - i))
if height[i] < height[j]:
i += 1
else:
j -= 1
return max_area
if __name__ == "__main__":
height = [1, 2, 3, 4, 3, 2, 1, 5]
result = Solution().maxArea(height)
print result
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/wd6/LeetCode-1.git
git@gitee.com:wd6/LeetCode-1.git
wd6
LeetCode-1
LeetCode-1
master

搜索帮助