题目

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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.

代码:oj测试通过 Runtime: 132 ms

 class Solution:
# @return an integer
def maxArea(self, height):
# none case or one element case
if height is None or len(height)<2:
return 0
# left point and right point
left = 0
right = len(height)-1
max_container = 0
while left<right :
curr_container = min(height[left],height[right]) * (right-left)
max_container = max(curr_container,max_container)
if height[left]>height[right]:
right = right-1
else:
left = left+1
return max_container

思路

数组前后双指针技巧。

有点儿像动态规划。

两个指针一左一右left right

面积为:min(height[left],height[right])*(right-left)

指针迭代条件为:哪边的指针所指位置的高度小,就从哪边往中间移动。每一步更新一次max_container的值。

为什么哪边的指针所指的位置高度小就从哪边往中间移动呢?能装多少水是有较短的那边决定的,因此如果寻求装更多的水,则应该优先从较短的一侧开始求变。

这样一来,每一次迭代后,都保证max_container保存了当前以及之前的可能最大蓄水量。

leetcode 【 Container With Most Water 】python 实现的更多相关文章

  1. leetcode Container With Most Water python

    class Solution(object): def maxArea(self, height): """ :type height: List[int] :rtype ...

  2. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  3. [LeetCode] Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  4. [LeetCode] Container With Most Water 简要分析

    前言 这题非要说贪心的话也算是吧,不过最主要的特征还是双指针.LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的.这道题倒是“短板效应”的不错体现了. 题目 题目链接 Given n non-neg ...

  5. [LeetCode]Container With Most Water, 解题报告

    前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n no ...

  6. C++LeetCode:: Container With Most Water

    本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做.感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过.每天都要被LeetCo ...

  7. leetcode Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  8. [Leetcode] Container With Most Water ( C++)

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  9. LeetCode——Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  10. LeetCode Container With Most Water (Two Pointers)

    题意 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...

随机推荐

  1. android通过fiddler代理,抓取网络请求

    安装fiddler过程省略 1, 2, 3, 4,手机需要跟电脑处于同一局域网,设置网络代理为电脑在局域网内的ip,端口为3步设置的port 5,电脑就可以通过fiddler监控手机的所有网络请求了( ...

  2. 开发Maven插件

    Mojo: Maven plain Old Java Object 1.插件命名规则:maven-<yourplugin>-plugin是Maven的保留字段,不允许使用,我们可以用< ...

  3. 关于docker容器内核参数修改问题

    以下内容截取自docker官方文档 地址:https://docs.docker.com/edge/engine/reference/commandline/run/#configure-namesp ...

  4. HDU 1008 电梯( 水题)

    题意: 电梯当前在0层,每在1层需要停5秒,往上走每层需6秒,往下走每层需要4秒. 思路: 在接收输入的时候直接计算了它,不用再弄一个循环.每计算一个请求就更新当前层,停留5秒的等到输出时再加上5*n ...

  5. World Wind Java开发之十四——添加WMS地图服务资源(转)

    数据是GIS的核心,没有数据一切无从谈起,Internet上有很多在线WMS地图服务资源,我们可以好好利用这些数据资源,比如天地图.必应地图.NASA.OGC数据服务等等. 在我们国家常用的还是天地图 ...

  6. objective C 内存管理及属性方法具体解释

    oc为每一个对象提供一个内部计数器.这个计数器跟踪对象的引用计数,当对象被创建或拷贝时.引用计数为1.每次保持对象时,调用retain接口.引用计数加1.假设不需要这个对象时调用release,引用计 ...

  7. 【CF1000C】Covered Points Count(离散化+差分)

    点此看题面 大致题意: 给出\(n\)条线段,分别求有多少点被覆盖\(1\)次.\(2\)次...\(n\)次. 正常的算法 好吧,这道题目确实有个很简单的贪心做法(只可惜我做的时候没有想到,结果想了 ...

  8. Zuul 网关搭建

    本机IP为  192.168.1.102 1.    新建 Maven 项目   zuul 2.   pom.xml <project xmlns="http://maven.apac ...

  9. shiro学习记录(二)

    1 在项目中应用shiro框架进行认证 第一步:引入shiro框架相关的jar <!-- 引入shiro框架的依赖 --> <dependency> <groupId&g ...

  10. 安装mysql提示This application requires .NET framework 4.0.

    问题描述:安装MySQL社区版时遇到This application requires .NET framework 4.0. 解决方法:在http://search.microsoft.com/zh ...