题目:

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.

题解:

这道题挺类似二分查找法的,这道题是先从两头开始算面积,面积的计算要由短板决定,并维护一个当前最大面积。

然后逐步替换小的短板来计算面积。每一步只替换短板的原因是,短板决定面积,而高板不影响,所以要想有所改变就改变那个有决定性的东西。。

网上有好多人都画出图来了。。可以搜搜看。。。

代码如下:

 1     public int maxArea(int[] height) {
 2         if(height == null || height.length == 0)
 3             return 0;
 4         
 5         int low = 0, high = height.length -1 ;  
 6         int max = 0;  
 7         while (low < high) {
 8          int area = (high-low)*Math.min(height[low], height[high]);
 9          
          max = Math.max(max, area);  
          if (height[low] < height[high])  
              low++;  
          else  
              high--;  
         }  
             return max;  
     }

Container With Most Water leetcode java的更多相关文章

  1. Container With Most Water - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Container With Most Water - LeetCode 注意点 没什么好注意的... 解法 解法一:暴力求解,假设任意两个端点会是最佳答 ...

  2. Container With Most Water -- LeetCode 11

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

  3. Container With Most Water——LeetCode

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

  4. Trapping Rain Water leetcode java

    题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...

  5. 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 ...

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

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

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

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

  8. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

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

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

随机推荐

  1. 同步VDP时间

    使用yast 进入蓝屏界面,修改system—date and time,取消hardware clock set to utc,时区设置为上海或者北京,然后sntp -r 时间服务器地址 敲击syn ...

  2. bzoj 1658: [Usaco2006 Mar]Water Slides 滑水

    题解: 很神奇的做法,把点分成入度大于出度和入度小于出度两种. 然后入度大于出度的点必须走到某个点,所以排序贪心. #include<stdio.h> #include<iostre ...

  3. hdu 4545 贪心 *

    题意:小明和他的好朋友小西在玩一个新的游戏,由小西给出一个由小写字母构成的字符串,小明给出另一个比小西更长的字符串,也由小写字母组成,如果能通过魔法转 换使小明的串和小西的变成同一个,那么他们两个人都 ...

  4. 2、Redis的基础知识

     写在前面的话:读书破万卷,编码如有神 -------------------------------------------------------------------- 主要内容包括: red ...

  5. [转]Android Message.obtain() 和Handler.obtainMessage()的区别

        目录(?)[+]   参考:http://www.2cto.com/kf/201311/255885.html http://www.cnblogs.com/over140/archive/2 ...

  6. centos7安装zookeeper3.4.9集群

    本篇文章目的:以最小成本学习zookeeper的集群安装. zookeeper的三要素: 1.一致,能够保证数据的一致性 2.有头,始终有一个leader,node/2+1个节点有效,就能正常工作 3 ...

  7. php中赋值和引用真真的理解

    php的引用(就是在变量或者函数.对象等前面加上&符号) //最重要就是 删除引用的变量 ,只是引用的变量访问不了,但是内容并没有销毁 在PHP 中引用的意思是:不同的名字访问同一个变量内容. ...

  8. redux基础(1)

    redux ps:每个案例都是接着上一个案例写的 主要以案例讲解如何使用,具体概念请参考如下: 基本概念参考1 基本概念参考2 案例源码戳这里 一.Store.Action.Reducer简介 Sto ...

  9. 【原】Order属性决定了不同切面类中通知执行的先后顺序

    [障碍再现] MyBatis配置多数据源时,数据源切换失败. [原因分析]    自定义切面和Spring自带事务切面“即<aop:advisor>”执行的先后顺序导致数据源不能切换成功. ...

  10. Git_分支管理

    分支就是科幻电影里面的平行宇宙,当你正在电脑前努力学习Git的时候,另一个你正在另一个平行宇宙里努力学习SVN. 如果两个平行宇宙互不干扰,那对现在的你也没啥影响.不过,在某个时间点,两个平行宇宙合并 ...