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. 关于 warning CS0659:“***”重写Object.Equals(object o)但不重写Object.GetHashCode()

    对象相等性和同一性 System.Object 类型提供了以下方法, namespace System { // // 摘要: // 支持 .NET Framework 类层次结构中的所有类,并为派生 ...

  2. Python面向对象相关知识1

    1. python是动态的语言,这样在使用类的时候,类的属性就可以随意的添加,但是这样在实际开发中有一定的缺陷,所以,可以在类中定义一个特殊的__init__()方法,当创建实例时,__init__( ...

  3. 关于js闭包的一些浅层面的理解

    function a() { var i = 0; function b() { alert(++i); } return b; } var c = a(); c(); 1.js分全局作用域和函数作用 ...

  4. 读书笔记 Week6 2018-4-12

    Chap 24 重构 读书笔记 一.需求的变更 单纯就科目学习中的小项目来说,目标在一开始便被明确下来,即可定义一份严谨的列表来描述功能.故在原来的编程经历中,只要上交了程序便一切都没事儿了,也没有重 ...

  5. C++11之 auto

    [C++11类型推导] 1.使用auto的时候,编译器根据上下文情况,确定auto变量的真正类型.auto在C++14中可以作为函数的返回值,因此auto AddTest(int a, int b)的 ...

  6. 利用UUID 随机生成8位短号

    //获得8位短号 public static String[] chars = new String[] { "a", "b", "c", ...

  7. 27-水池数目(dfs)

    水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地 ...

  8. 如何启动jdeveloper中集成的weblogic

    1>运行jdeveloper,打开运行日志,入下图,日志最开始的红框部分就是打开weblogic的命令,将此命令复制出来执行即可打开weblogic 程序员的基础教程:菜鸟程序员

  9. 要生成在[min,max]之间的随机整数,

    import java.util.Random; public class RandomTest { public static void main(String[] args) { int max= ...

  10. 用 python 实现各种排序算法-乾颐堂

    总结了一下常见集中排序的算法 归并排序 归并排序也称合并排序,是分治法的典型应用.分治思想是将每个问题分解成个个小问题,将每个小问题解决,然后合并. 具体的归并排序就是,将一组无序数按n/2递归分解成 ...