Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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.

贪心策略:两端设置围栏,无论如何注水,水只要不超过其中较短的那一根围栏即可,中间的柱子可以忽略。

相继缩短距离,较短一端的柱子换成向里的一根。

例如: height[left] <= height[right] :  left = left+1;

        反之, right = right-1;

 class Solution {
public:
int maxArea(vector<int> &height) {
int ans=,left=,right=height.size()-,contain;
while(left<right)
{
contain = (right-left)*min(height[right],height[left]);
ans = max(contain, ans);
height[left]<=height[right]?left++:right--;
}
return ans;
}
};

[LeetCode 题解]: Container With Most Water的更多相关文章

  1. leetcode题解||Container With Most Water问题

    problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate ...

  2. LeetCode题解——Container With Most Water

    题目: x轴上有一些点,每个点上有一条与x轴垂直的线(线的下端就是这个点,不超出x轴),给出每条线的高度,求这些线与x轴组成的最大面积. 解法: 贪心策略,维持两个指针,分别指向第一个和最后一个元素, ...

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

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

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

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

  5. 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 用双指针向中间滑动,较小的高度就作为当前情 ...

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

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

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

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

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

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

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

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

随机推荐

  1. sublime text 怎么浏览包

    点击到设置里 里面有个包浏览.

  2. Error: listen EACCES 0.0.0.0:8080 错误解决记录

    live-server -- 热加载利器 实现本地服务器,可及时刷新. 1.通过npm install -g live-server进行安装 2.npm init 初始化项目3.在所需要的文件夹内运行 ...

  3. RPC通信

    @version: @author: morgana @license: Apache Licence @contact: vipmorgana@gmail.com @site: @software: ...

  4. C命令行参数

    总是忘了,在这里说明下. argc是命令行参数的实际个数,从1开始. 第一个是可执行文件的名称 argv[]的元素是字符串 每个元素是个命令行参数. #include<stdio.h> i ...

  5. 一个简单的语义分析算法:单步算法——Python实现

    以前 曾经有一个人教会我一件事 要学会相信一些看似不可能的事  当你真的相信的时候  或许 没有什么事情是不可能的 ——<秦时明月•与子同归> 在编译原理的众多书籍中,陈述了很多生成语法树 ...

  6. python拷贝目录下的文件

    #!/usr/bin/env python # Version = 3.5.2 import shutil base_dir = '/data/media/' file = '/backup/temp ...

  7. Angularjs Ng_repeat中实现复选框选中并显示不同的样式

    最近做了一个选择标签的功能,把一些标签展示给用户,用户选择自己喜欢的标签,就类似我们在购物网站看到的那种过滤标签似的: 简单的效果如图所示: 首先看一下html代码: 1 <!DOCTYPE h ...

  8. a+b_1

    题目截图: 思路: 直接输出即可. 代码如下: /* a+b */ #include <stdio.h> #include <string.h> #include <ma ...

  9. MongoDB数据仓储

    本篇是作为另一篇随笔的一部分‘搭建一个Web API项目’ MogonDB官网:https://www.mongodb.org/ 安装过程参考园友的分享http://www.cnblogs.com/l ...

  10. firebug,chrome调试工具的使用

    ​http://ued.taobao.org/blog/?p=5534 chrome调试 http://www.cnblogs.com/QLeelulu/archive/2011/08/28/2156 ...