题目来源:

https://leetcode.com/problems/container-with-most-water/


题意分析:

给出一个n长度的非0数组,a1,a2,……,an,ai代表在坐标i上的高度为ai。以以ai,aj为高,i到j为底,可以构造出一个容器。那么求出这些容器中可以装的水的最大容积(容器不能倾斜)。例如数组[2,1],一共可以构造1个容器,这个容器的四个端点坐标是(0,0),(0,2),(1,1),(1,1),那么他可以装的最大的水容积是(1-0)*1 = 1.


题目思路:

     我们不难发现,水容积的大小是由短高度决定的。暴力的方法就是把所有的容器找出来,算出他们的水容积,一一比较,然后得到最大值,这种方法的时间复杂度是(O(n^2))。很明显会TLE。

我们认真研究一下寻找过程,我们从第一个高度为起始容器壁,那么我们直接以最后一个高度为终止壁,如果a1 <= an,那么以a1为起始的容器最大是a1 * (n - 1),以a1为容器壁的最大容器计算出来的。那么以a1为壁的所有情况不需要再考虑,接着考虑a2的;同理,如果a1 > an,an不再考虑,考虑an-1这有点类似"夹逼定理"。比较ai和aj(i<j)如果ai <= aj,i++;否者j ++直到i == j。这个算法的时间复杂度是(O(n))。


代码(python):

 class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
size = len(height) # the size of height
maxm = 0 # record the most water
j = 0
k = size - 1
while j < k:
if height[j] <= height[k]:
maxm = max(maxm,height[j] * (k - j))
j += 1
else:
maxm = max(maxm,height[k] * (k - j))
k -= 1
return maxm

转载请注明出处:http://www.cnblogs.com/chruny/p/4817787.html

[LeetCode]题解(python):011-Container With Most Water的更多相关文章

  1. No.011 Container With Most Water

    11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...

  2. LeetCode--No.011 Container With Most Water

    11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...

  3. LeetCode Array Medium 11. Container With Most Water

    Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...

  4. 【LeetCode】011 Container With Most Water

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

  5. 【JAVA、C++】LeetCode 011 Container With Most Water

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

  6. [Leetcode]011. Container With Most Water

    public class Solution { public int maxArea(int[] height) { int left = 0, right = height.length - 1; ...

  7. leetcode第11题--Container With Most Water

    Problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate ...

  8. 【LeetCode two_pointer】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, ai ...

  10. LeetCode(11) Container With Most Water

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

随机推荐

  1. Stitch Fix 融资1200万美元,又一个时尚创业的哈佛女MBA |华丽志

    Stitch Fix 融资1200万美元,又一个时尚创业的哈佛女MBA |华丽志 Stitch Fix 融资1200万美元,又一个时尚创业的哈佛女MBA

  2. struts Value Stack 值栈

    首先声明:本文是从博客园博友的文章转载过来的,感觉说的不错.在此附上地址:http://www.cnblogs.com/jerryxing/archive/2012/04/23/2467299.htm ...

  3. POJ1363:Rails

    Description There is a famous railway station in PopPush City. Country there is incredibly hilly. Th ...

  4. $timeout, $interval

    $timeout, $interval   layout: posttitle: Angular@1.4.3 中文 API 服务篇 $timeout & $intervaldesc: '$ti ...

  5. Visual format language

    所谓的VFL语言其实就是Visual format language 的缩写,是一种使用代码添加约束的方式,类似于Masonry  SDAutolayout的效果,但是操作起来可能要相对简单.一行代码 ...

  6. C/C++修改常量的值

    C/C++中常量修饰const可以用来保证一些确定的量不会被一不小心改变,比如PI,一直是3.14159...... 但是不排除有时候也会需要修改常量的值,通过直接修改是不能达到目的. 比如: #in ...

  7. ThinkPHP第十九天(Ueditor高亮插件、扩展函数载入load、静态缓存)

    1.使用Ueditor编辑器,插入代码后,显示的时候高亮显示,需要调用Ueditor中的第三方插件third-party中的SyntaxHighlighter 调用方法: 引入CSS和JS文件,并调用 ...

  8. 无法在web服务器上启动调试,服务器不支持对ASP.NET 或ATL Server应用程序进行调试。

    无法在web服务器上启动调试,服务器不支持对ASP.NET 或ATL Server应用程序进行调试. a>.DCOM配置里的属性灰色不可用的解决方法,   1>.管理工具->组件服务 ...

  9. winform代码:关联窗体数据更新,删除dataGridview中选中的一行或多行

    一.关联窗体数据更新 关联窗体数据修改时,如果一个为总体数据显示窗体A,另一个为详细修改窗体B,从A进入B,在B中对数据进行修改,然后返回A,这时A窗体的数据需要更新. 我采用最简单的方法,首先保证每 ...

  10. 0610 python 基础03

    复习: 条件判断 if..else >>> age=28 >>> if age<18: ...   print "你还没有成年吧" ... ...