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. Python 学习笔记(一)Python 简介

    Python 简介 Python  1989年 吉多 · 范罗苏姆(Guido van Rossum)发明 Python 是一种解释型.面向对象.动态数据类型的高级程序设计语言 Python 应用于众 ...

  2. [POI2007]ATR-Tourist Attractions [TPLY]

    [POI2007]ATR-Tourist Attractions 题目链接(https://www.luogu.org/problemnew/show/P3451) 这种稠密图还是建议你不要跑spfa ...

  3. C#多线程编程(6)--线程安全2 互锁构造Interlocked

    在线程安全1中,我介绍了线程同步的意义和一种实现线程同步的方法:volatile.volatile关键字属于原子操作的一种,若对一个关键字使用volatile,很多时候会显得很"浪费&quo ...

  4. MySQL性能调优与架构设计

    1.Scale(扩展):从数据库来看,就是让数据库能够提供更强的服务能力 ScaleOut: 是通过增加处理节点的方式来提高整体处理能力 ScaleUp: 是通过增加当前处理节点的处理能力来提高整体的 ...

  5. 【noip模拟】局部最小值

    TimeLimit: 1000ms               MemoryLimit: 256MB Description 有一个n行m列的整数矩阵,其中1到n×m之间的每个整数恰好出现一次.如果一 ...

  6. LCA倍增算法

    LCA 算法是一个技巧性很强的算法. 十分感谢月老提供的模板. 这里我实现LCA是通过倍增,其实就是二进制优化. 任何一个数都可以有2的阶数实现 例如16可以由1 2 4 8组合得到 5可以由1 2 ...

  7. 洛谷 P2904 [USACO08MAR]跨河River Crossing

    题目 动规方程 f[i]=min(f[i],f[i−j]+sum) 我们默认为新加一头牛,自占一条船.想象一下,它不断招呼前面的牛,邀请它们坐自己这条船,当且仅当所需总时间更短时,前一头奶牛会接受邀请 ...

  8. PORTE_ISFR & (1<<n)

    位运算关键:空出补零,溢出舍弃

  9. 直播-rtmp学习

    RTMP(实时消息传输协议),官方介绍如下: Adobe’s Real Time Messaging Protocol (RTMP), an application-level protocol de ...

  10. 【Unity与23种设计模式】代理模式(Proxy)

    GoF中定义: "提供一个代理者位置给一个对象,好让代理者可以控制存取这个对象." 代理模式多用来优化资源的加载 当资源已经加载过时,使用原来的资源 当资源没有加载过时,开始加载 ...