这个问题是芭芭拉在采访中遇到的,不幸的是,的复杂性O(n2)该,太失望了,难怪没有通过面试。

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.

题目说的有点复杂,大意是利用x轴作底,两个随意的竖直线段作杯壁,何时盛水最多。

木桶原理大家肯定都知道,水盛多少取决于最短的杯壁,所以此题还能够引申为往围成的区域内放矩形。如何使得矩形面积最大。

题目中的不能倾斜(slant:倾斜,倾倒)相应为矩形必须水平放置。

复杂度为O(n)的思想是贪心原理,先从底边最大的情况考虑,计算最大面积后。此时要将底边长度减1,仅仅须要将杯壁较短的那一边移动一个单位距离,得到的解必然优于杯壁较长那边移动的情况。这样保证每次移动都得到的是局部最优解。

class Solution {
public:
int maxArea(vector<int> &height) {
int Len = height.size(),low=0,high=Len-1;
int maxV = 0;
while(low<high){
maxV = max(maxV,(high-low)*min(height[low],height[high]));
if (height[low]<height[high]) low++;
else high--;
}
return maxV;
}
};

版权声明:本文博客原创文章,博客,未经同意,不得转载。

【LeetCode】【Python解读】Container with most water的更多相关文章

  1. leetcode面试准备:Container With Most Water

    leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...

  2. LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number

    1.  Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...

  3. 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  4. 【LeetCode】11. Container With Most Water 盛最多水的容器

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...

  5. LeetCode(11)题解: Container With Most Water

    https://leetcode.com/problems/container-with-most-water/ 题目: Given n non-negative integers a1, a2, . ...

  6. 【LeetCode】11. Container With Most Water

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

  7. leetcode problem 11 Container With Most Water

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

  8. LeetCode OJ 11. Container With Most Water

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

  9. Leetcode Array 11 Container With Most Water

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

  10. 【LeetCode】011 Container With Most Water

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

随机推荐

  1. mindmanager2012打开文件出现runtime error r6025 解决方式

    关于mindmanager 2012启动无法执行,提示c++错误 ---------------------------Microsoft Visual C++ Runtime Library---- ...

  2. 斯坦福ML公开课笔记14——主成分分析

    上一篇笔记中,介绍了因子分析模型,因子分析模型使用d维子空间的隐含变量z来拟合训练数据,所以实际上因子分析模型是一种数据降维的方法,它基于一个概率模型,使用EM算法来预计參数. 本篇主要介绍PCA(P ...

  3. iOS开发人员必看的精品资料(100个)

    目录下载地址:http://down.51cto.com/data/416672 附件预览如下: iOS游戏开发之Unity3D引擎系列 http://down.51cto.com/zt/399 iP ...

  4. 原声JS瀑布流加延迟载入

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Hibernate常用Annotation标签说明

    @ javax.persistence.Entity 实体类定义,该标签表示当前类是一个Hibernate的数据库实体,对应着数据库中的某个表 位置:用于类级别 参数:无 样例:@Entity 注意: ...

  6. 《JavaScript设计模式与开发实践》读书笔记之观察者模式

    1.观察者模式 观察者模式定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都将得到通知. JavaScript中通常采用事件模型替代传统的观察者模式 1.1 逐步实现观 ...

  7. MD5加密,解密

    using System; using System.Collections.Generic; using System.Text; using System.Globalization; using ...

  8. boost事件处理

    尽管这个库的名字乍一看好象有点误导,但实际上并不是如此. Boost.Signals 所实现的模式被命名为 '信号至插槽' (signal to slot).它基于下面概念:当相应的信号被发出时.相关 ...

  9. [Java] HttpClient有个古怪的stalecheck选项

    打开stale check会让每次http请求额外消耗15毫秒.而且stalecheck选项缺省是打开的. 这有必要吗???? 在局域网里面调用web api service的时候会死人的. http ...

  10. C# 如何获取某用户的“我的文档”的目录

    Console.WriteLine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)); System.E ...