题目来源:

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. Android Material Design 之 Toolbar的使用

    Material Design是谷歌提出来的最新ui设计规范,我想actionbar大家也许并不陌生,toolbar简单而言可以看做为actionbar的升级版,相比较actionbar而言,tool ...

  2. Springmvc+Spring+Hibernate搭建方法及实例

    Springmvc+Spring+Hibernate搭建方法及实例  

  3. slave 成为master 时候执行的操作notify_master /etc/keepalived/send_master.sh

    slave:/root# cat /etc/keepalived/keepalived.conf global_defs { router_id MySQL-ha } vrrp_instance VI ...

  4. 关于typedef之四种用途 和 两个陷进

    typedef用来声明一个别名,typedef后面的语法,是一个声明.本来笔者以为这里不会产生什么误解的,但结果却出乎意料,产生误解的人 不在少数.罪魁祸首又是那些害人的教材.在这些教材中介绍type ...

  5. NHibernate初入门之映射文件配置说明(三)

    转载逆心http://www.cnblogs.com/kissdodog/archive/2013/02/21/2919886.html 1. hibernate-mapping 这个元素包括以下可选 ...

  6. Bootstrap 源码解析

    前言 Bootstrap 是个CSS库,简单,高效.很多都可以忘记了再去网站查.但是有一些核心的东西需要弄懂.个人认为弄懂了这些应该就算是会了.源码看一波. 栅格系统 所谓的栅格系统其实就是一种布局方 ...

  7. Jquery $.extend的重载方法详述

    1 $.extend(result,item1,item2,item3,........)  -这个重载方法主要是用来合并,将所有的参数都合并到result中,并返回result,但是这样会破坏res ...

  8. 内置的材质包含文件 .cginc

    Unity中包含几个文件,可以用于你的Shader程序,里面包含了预定义的变量和辅助函数.使用它需要用 #include指令 CGPROGRAM // ... #include "Unity ...

  9. c++ primer plus 习题答案(2)

    p221.8 #include<iostream> #include<cstdlib> #include<cstring> using namespace std; ...

  10. Problem C: Andy's First Dictionary

    Problem C: Andy’s First DictionaryTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 18 Solved: 5[Submit] ...