Description: 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.

问题:对于非负整数数组:[a1,a2,a3,…,an],点(i,0)(i,ai)组成线段i,点(j,0)(j,aj)组成线段j,这两条线段与x轴组成一个容器,求解使得容器可容纳水最多时的截面面积Area=|i-j|*min(ai,aj)。

以下描述中假定i<j

解法1(暴力解法):

拿到题目首先会想到使用暴力解法,即算出所有线段之间可能组成的面积,再找到其中的最大面积。

Python:

class  Solution(object):
def maxArea(self, height):
length = len(height)
MaxArea = 0
for i in range(length):
j = i+1
while(j<length):
MaxArea = max(MaxArea, (j - i)*min(height[i], height[j]))
j = j+1
return MaxArea

当提交代码后会提示超时。显而易见当数组height很大时,使用该方法需要计算出所有结果,算法时间复杂度为:O(n^2)

思考一下:如何减少计算次数,使算法复杂度为O(n)?即能否只遍历一次数组就能找到最大值?对于面积Area=(j-i)*min(ai,aj),假设min(ai,aj)=ai时,当(j-i)越大Area值才会越大。即应选择离ai最远的大于ai的元素。

解法2

参考上述思路,设置两个指针i,j指向数组最左端与最右端,当最左端值小于最右端时,保存面积Area = (j-i)*min(height[i],height[j]),接着指针i向右移动,继续比较。当最右端小于最左端时,保存面积,接着指针j向左移动,继续比较。

Python:

class Solution(object):
def maxArea(self, height):
i, j = 0, len(height) - 1
MaxArea = 0
while (i < j):
MaxArea = max(MaxArea, (j - i)*min(height[i], height[j]))
if height[i] < height[j]:
i=i+1
else:
j=j-1
return MaxArea

LeetCode---Container With Most Water(11)的更多相关文章

  1. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

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

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

  3. [LeetCode]Container With Most Water, 解题报告

    前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n no ...

  4. C++LeetCode:: Container With Most Water

    本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做.感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过.每天都要被LeetCo ...

  5. leetcode Container With Most Water

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

  6. [LeetCode] Container With Most Water 简要分析

    前言 这题非要说贪心的话也算是吧,不过最主要的特征还是双指针.LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的.这道题倒是“短板效应”的不错体现了. 题目 题目链接 Given n non-neg ...

  7. [Leetcode] Container With Most Water ( C++)

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

  8. LeetCode——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 (Two Pointers)

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

  10. [Leetcode] container with most water 最大水容器

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

随机推荐

  1. iOS - EaseMob 环信的使用

    1.环信 环信使用的是 XMPP 协议,它是在 XMPP 的基础上进行的二次开发,对服务器 Openfire 和客户端进行功能模型的添加和客户端 SDK 的封装.环信的本质还是使用 XMPP,基于 S ...

  2. 用Python来找合适的妹子

    时间真的有点仓促,匆匆忙忙撸完这篇文章. 虽然今天是情人节,但还是要关心一下单身狗们,帮助他们俩脱单. 古人云:知己知彼,百战不殆.  好好去了解一下妹子们的内心想法,早日脱单! 这次我在一个某知名婚 ...

  3. [TJOI2016&&HEOI2016]求和

    BZOJ Luogu 求 \[f(n)=\sum_{i=0}^{n}\sum_{j=0}^{i}S(i,j)*2^j*j!\] 其中\(S(i,j)\)是第二类斯特林数 \(n\le10^5\),模\ ...

  4. Micropython Turnipbit 换挡风扇 旋转按钮控制直流电机转速

    学过物理学的我们都知道换挡风扇的原理,一般按钮控制电感分压或者电容分压,以达到控制电流的目的.那么我们可不可以使用Turnipbit模拟这个系统呢?其实是很简单的.类似于之前用Tpyboard做的智能 ...

  5. Java实现mongodb原生增删改查语句

    Java实现mongodb原生增删改查语句 2018-03-16 自动化测试时,需校验数据库数据,为了快速自动化,在代码中用原生增删改查语句操作mongodb 结构 代码 0 pom.xml < ...

  6. intellij idea快捷键字典

    最近在重装系统,在安装python IDE时候依然安装了sublime Text3和intellij Idea(冏,别问为什么没安装pycharm,0-0 逃).首先是已然将之前一直使用的sublim ...

  7. 设计模式——外观模式(C++实现)

    #include <string> #include <iostream> using namespace std; class STSystemA { public: voi ...

  8. CPU平均负载 load average

    平均负载是指上一分钟同时处于就绪状态的平均进程数.在CPU中可以理解为CPU可以并行处理的任务数量,就是CPU个数X核数.如果CPU Load等于CPU个数乘以核数,那么就说CPU正好满负载,再多一点 ...

  9. Flume搭建

    [root@hadoop01 src]# mkdir flume [root@hadoop01 src]# ll 总用量 0 drwxr-xr-x. 2 root root  6 4月   7 201 ...

  10. python 中的 args,*args,**kwargs的区别

    一.*args的使用方法 *args 用来将参数打包成tuple给函数体调用 例子一:def function(*args):      print(args, type(args))function ...