题目

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. wechat开发笔记之1.接口示例代码

    修改后的php示例代码! <?php /** * wechat php test */ //define your token define("TOKEN", "w ...

  2. Win10 应用商店管理应用

    在企业日常办公中,对 Windows 10 应用商店软件不需要,希望办公系统干净一些.企业运维中,我们可以使用组策略来管理Windows 10 微软Store应用程序.可以根据组织的要求进行配置,多项 ...

  3. 使用CreateProcess函数运行其他程序

    为了便于控制通过脚本运行的程序,可以使用win32process模块中的CreateProcess()函数创建一个运行相应程序的进程.其函数原型如下.CreateProcess(appName, co ...

  4. hihoCoder 挑战赛10 #1144 : 01串

    思路:这只是逻辑测试题吧,考虑周全就行了.考虑n=m的情况,n>m的情况,m>n的情况. (1)n>m的情况,0比1多几个都是行的,一共有m个“01”,后面补足够多个零即可. (2) ...

  5. 【BZOJ2754】[SCOI2012] 喵星球上的点名(后缀数组+莫队)

    点此看题面 大致题意: 每个人的名字由姓和名构成,如果某次点名点到的字符串是某人姓或名的一个子串,则这个人就被点到了.求每次点名被点到的人的个数及每个人被点到的总次数. 后缀数组+莫队 这道题做法很多 ...

  6. Nginx+proxy实现简单的负载均衡

    环境说明:操作系统centos6.6 64位web操纵系统是:web1=192.168.10.10(LAMP) web2=192.168.10.11(LNMP),这里只是测试nginx实现负载均衡效果 ...

  7. window下安装ubuntu(ubuntu可删除)

    进入ububtu13.04的安装界面,这里我们选择了“中文(简体)”,然后单击安装: 下图是现场拍的:   出现如下图时,请根据需要选择,然后单击“继续” , 接下来会出现问你是否要连接网络,我们选择 ...

  8. Java 替换word文档文字,指定位置插入图片

    先说下 需要的依赖包 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ex ...

  9. 5.Cisco Packet Tracer里关于交换机或路由器配置文件和系统映像备份与恢复

    我们会将交换机或路由器的配置文件和系统镜像直接备份到tftp服务器上,所以我们需要准备一台tftp的服务器 1我们需要给服务器配一个ip地址,给路由器的f0/1端口配置一个ip地址,路由器与服务器能相 ...

  10. Lake Counting(dfs)

    Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...