# -*- 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. PHP 魔术常量__FUNCTION__与__METHOD__的区别

    __FUNCTION__ 返回 函数名称(PHP 4.3.0 新加).自 PHP 5 起本常量返回该函数被定义时的名字(区分大小写).在 PHP 4 中该值总是小写字母的.    __METHOD__ ...

  2. jquery的冒泡和默认行为

    如果在页面中重叠了多个元素,并且重叠的这些元素都绑定了同一个事件,那么就会出现冒泡问题. //HTML 页面 <div style="width:200px;height:200px; ...

  3. VB2012读取xml

    上回谢了生成写xml的,现在把读取的补上 文件如下 <?xml version="1.0" encoding="UTF-8" standalone=&qu ...

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

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

  5. css案例学习之class执行的顺序

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 微信的分享功能(针对web手机站页面进行的分享功能)

    把这段js粘贴进,设置可以分享的页面,当微信打开,即可微信进行分享各个圈 $(function(){ var lujing=$("#logimg").attr("src& ...

  7. mit java open course assignment #4

    package come; public class Library { // Add the missing implementation to this class String realLoca ...

  8. window快捷登陆linux的的设置方式(设置ssh的config配置)

    看看网上其他人如何写的: http://www.xuebuyuan.com/414672.html 文中~的意思是用户目录下的意思: http://blog.csdn.net/newjueqi/art ...

  9. C#反射概念以及实例详解【转】

    2009-08-28 13:12 佚名 互联网 我要评论(1) 字号:T | T C#反射概念以及实例向你介绍了C#反射的基本内容以及C#反射实例的简单应用,希望对你了解和学习C#反射以及C#反射实例 ...

  10. Linux下patch打补丁命令

    此命令用于为特定软件包打补丁,他使用diff命令对源文件进行操作. 基本命令语法: patch [-R] {-p(n)} [--dry-run] < patch_file_name p:为pat ...