前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetcode: 11. Container With Most Water

leetcode: 11. Container With Most Water

作者头像
JNingWei
发布2018-09-28 14:46:49
3890
发布2018-09-28 14:46:49
举报

Problem

# 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 and n is at least 2.

AC

class Solution():
    def maxArea(self, height):
        max_area, left, right = 0, 0, len(height) - 1
        while left < right:
            max_area = max(max_area, min(height[left], height[right]) * (right - left))
            if height[left] < height[right]:
                left += 1
            else:
                right -= 1
        return max_area


if __name__ == "__main__":
    height = [1, 2, 3, 4, 3, 2, 1, 5]
    assert Solution().maxArea(height) == 16
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年10月31日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem
  • AC
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档