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

不同高度的柱子排一列,两个柱子可以组成一个容器,求最大容积。

最直观的方法就是暴力,两层for循环,分别遍历头柱子和尾柱子。但是超时了

于是看了discuss,有O(n)的方法。

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

证明的话如下:对于low 和high,当 height[low]<hight[high]的时候,low往前前进了一个,这个时候就相当于对 height[low],hight[high-1]没有检查,而如果height[high-1]比hieght[high]大,则还是以height(low)为准,但是宽度上减小了一个,所以面积小了,如果height[high-1]小于height[high]则面积就更小了。所以这个漏掉是安全的。

LeetCode OJ-- Container With Most Water的更多相关文章

  1. LeetCode OJ Container With Most Water 容器的最大装水量

    题意:在坐标轴的x轴上的0,1,2,3,4....n处有n+1块木板,长度不一,任两块加上x轴即可构成一个容器,其装水面积为两板的间距与较短板长之积,以vector容器给出一系列值,分别代表在0,1, ...

  2. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  3. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  4. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  5. [LeetCode][Python]Container With Most Water

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/container-with-most-water/ Given n non-neg ...

  6. LeetCode 11. Container With Most Water (装最多水的容器)

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

  7. leetcode 【 Container With Most Water 】python 实现

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

  8. [LeetCode] 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, ...

  10. LeetCode#11. Container With Most Water

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

随机推荐

  1. python字符串处理方法

    一.combine & duplicate 字符串结合和复制 字符和字符串可以用来相加来组合成一个字符串输出: 字符或字符串复制输出. 二.Extract &Slice 字符串提取和切 ...

  2. Java面向对象---抽象类与接口

    final关键字 1.final关键字在java中被称为完结器,表示最终的意思: 2.final能声明类.方法.属性: 3.使用final声明的类不能被继承,使用final声明的方法不能被重写,使用f ...

  3. Linux档案与文件系统的压缩与打包

    总结: 压缩指令为透过一些运算方法去将原本的档案进行压缩,以减少档案所占用的磁盘容量.压缩前与压缩后的档案所占用的磁盘容量比值,就可以被称为是“压缩比” 压缩的好处是可以减少磁盘容量的浪费,在www网 ...

  4. 使用数组的splice方法以及循环嵌套来实现数组去重

    javascript代码如下   <script type="text/javascript"> var arr = [1,2,3,4,5,6,5,4,3,7,8,9, ...

  5. 怎么使用瓦特平台下面的“代码工厂”快速生成BS程序代码

    这里说一下怎么使用瓦特平台下面的“代码工厂”快速生成程序代码 使用平台:windows+"visual studio 2010"+"SqlServer2000+" ...

  6. IOS开发---菜鸟学习之路--(十一)-使新闻内容自适应高度

    上一章当中,我们留了一个小BUG. 其实就是浏览新闻的时候,如果文字内容过长的花,UITextView 会有个下拉框,而最底层的UIScrollView也有个下拉框,那么在使用的时候就会非常的不爽. ...

  7. IOS开发---菜鸟学习之路--(十)-实现新闻详细信息浏览页面

    前面已经将了上下拉刷新 实现了上下拉刷新后我们的第一级界面就做好,接下来我们就需要实现 新闻详细信息浏览了 我个人认为一般实现新闻详细页面的方法有两种(主要是数据源的不同导致了方法的不同) 第一种是本 ...

  8. Python3.0-3.6的版本变化

    Table of Contents Python3.0 简单的变化 语法的变化 新语法 改动的语法 剩下的变化 Python3.1 Python3.2 Python3.3 Python3.4 Pyth ...

  9. TextBox的值随dropdownlist值而变化

    转自:http://bytes.com/topic/asp-net/answers/443065-textbox-value-change-select-other-item-dropdownlist ...

  10. Swift全栈开发

    前段时间学习了一下Swift web framework-Vapor, 类似于PHP Laravel的web框架. Apple也成立了Server APIs Project, Server-side ...