# -*- coding: utf8 -*-
'''
https://oj.leetcode.com/problems/container-with-most-water/ 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. ===Comments by Dabay===
这道题很经典。
原理:固定一根长杆子X的时候,最远端的一个短杆子Y,此时是Y能围住的最大容积。所以Y的全部可能都考虑进去了。可以考虑第二远的杆子了。
也就是说,先选择两端的杆子,固定长的那一个(因为长的杆子可能与另外某根杆子围成更大的容积),移动短的那个(因为以短的杆子能围住的最大可能容积,
就是在移动之前,与长杆围成的)。 两个指针,一个方向从左到右,另一个方向从右到左。
计算最大容量,更新max_area。
然后移动,小数的指针(即:如果左指针的数比右指针的数小,左指针往右移动,进入下一次计算;反之,右指针往左移动,进入下一次计算)
当两个指针遇到的时候退出。 这里的代码不是优美的,完全是为了跑个OJ靠前的位置。这基本上是python最靠前的成绩了。109ms。
'''
class Solution:
# @return an integer
def maxArea(self, height):
max_area = 0
i, j = 0, len(height) - 1
while i < j:
if height[i] < height[j]:
right_taller, short, short_index = True, height[i], i
else:
right_taller, short, short_index = False, height[j], j area = short * (j-i)
if area > max_area:
max_area = area if right_taller:
i = i + 1
while height[i] < short:
i = i + 1
else:
j = j - 1
while height[j] < short:
j = j - 1
return max_area def main():
s = Solution()
print s.maxArea([1,2,5,1,4,1]) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[LeetCode][Python]Container With Most Water的更多相关文章

  1. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  2. LeetCode OJ Container With Most Water 容器的最大装水量

    题意:在坐标轴的x轴上的0,1,2,3,4....n处有n+1块木板,长度不一,任两块加上x轴即可构成一个容器,其装水面积为两板的间距与较短板长之积,以vector容器给出一系列值,分别代表在0,1, ...

  3. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  4. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  5. leetcode 【 Container With Most Water 】python 实现

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

  6. LeetCode 11. Container With Most Water (装最多水的容器)

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

  7. 蜗牛慢慢爬 LeetCode 11. Container With Most Water [Difficulty: Medium]

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

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

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

  9. 【leetcode】Container With Most Water

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

随机推荐

  1. 如果有需要确解MD5的,可以尝试这个网站。

    http://www.hashkiller.co.uk/md5-decrypter.aspx

  2. 使用pymysql和paramiko实现远程安装软件

    通过pymysql模块调用数据库获取安装信息,通过paramiko模块远程传输脚本并执行来安装软件,本文以安装apache为例. 创建apache安装信息数据表install_apache,设定三个字 ...

  3. debian install & configure(2)-drivers-ati

    依赖apt-get install build-essential cdbs fakeroot dh-make debhelper debconf libstdc++6 dkms libqtgui4 ...

  4. mysql_config_editor程序的用法

    1.mysql_config_editor程序的作用: 它只是用来在用户的家目录下生成一个.mylogin.cnf 里面保存有用于登录mysql-server端的password,host,user信 ...

  5. 移动网络山寨版(OpenBTS)的意义或者无意义 【1】

    在美国内华达州北部,靠近加州的峡谷中,有一片平坦的沙漠,名叫黑岩沙漠(Black Rock Desert).自从1986年以来,每年夏天,在这片沙漠中,都会举办一个为期八天的狂欢节.这个狂欢节的名字叫 ...

  6. 项目与软件推荐之编辑器-QOwnNotes(刺激自己)

    项目与软件推荐之编辑器-QOwnNotes 今天推荐一款软件 QOwnNotes,是一款普通文本笔记软件.以某个路径为目录,罗列出目录下所有的 md 文件或者 txt 文件. 有如下亮点: 启动速度快 ...

  7. js计算时间差,包括计算,天,时,分,秒

    收集两个计算时间差的计算方法代码片段: var date1=new Date(); //开始时间 var date2=new Date(); //结束时间 var date3=date2.getTim ...

  8. 配置自己风格的Clang-Format-Xcode

    在项目根目录下,创建一个文件.clang-format,使用vim打开并修改. 具体的配置风格可以看这里:http://clang.llvm.org/docs/ClangFormatStyleOpti ...

  9. 在自定义的js验证规则中调用magento的VarienForm方法验证表单

    js部分<script type="text/javascript"> //<![CDATA[ var loginForm = new VarienForm('l ...

  10. POJ3322-经典的游戏搜索问题

    临近毕业真是各种琐事多,好不容易写完几万字蛋疼的论文,又接着户口档案挂靠,毕业旅游,20多个离校盖章,签证被check了几个星期还没消息,希望8月初能走啊. 各种事情之下,人就是懒加心散,好久没写代码 ...