题目:

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. SQL_异化

    select a.pk_accasoa from bd_accasoa a; --下级科目原来主键: 0001Z0100000000001A2 --执行该语句后下级科目异化了(替换的意思) , '@@ ...

  2. Adobe PS CS6安装详解

    Adobe PS CS6安装破解详解 注:电脑上是否拥有虚拟光驱,若是没有,推荐2345好压:官网http://haozip.2345.com/下载地址:http://dl.2345.com/haoz ...

  3. 1013 Battle Over Cities (25)(25 point(s))

    problem It is vitally important to have all the cities connected by highways in a war. If a city is ...

  4. 一列道出yield和生成器的真谛

    均匀大小的块 def chunks(l, n): """Yield successive n-sized chunks from l.""" ...

  5. Django Q对象

    使用Q 对象进行复杂的查询¶ filter() 等方法中的关键字参数查询都是一起进行“AND” 的. 如果你需要执行更复杂的查询(例如OR 语句),你可以使用Q 对象. Q 对象 (django.db ...

  6. Qt Quick快速入门之信号、槽

    信号和槽主要用于组件之间的通信,类似于.net和java中的委托. 使用QObject::connect方法将信号与槽关联起来,然后信号的发起者发出信号,接受者中的槽函数就会执行. 比如connect ...

  7. NOIP差不多可以退役的退役记录

    好吧,现在既然苟回来了,就来回忆一下我NOIP2017的黑历史吧. Day-1: 原本以为是很平静的一天,上午学考机房强行断网,原因是腾出网速给对面学考的同学们查资料用.好吧没法刷题我只能选择颓废…… ...

  8. 河南省队选拔 HAOI2015 解题报告

      其实省选在四天前就已经结束了,但由于题目难度略大我到今天上午才补完所有题目……(捂脸逃)考场上很幸运,打完了所有我会写的部分分,最后Round1的110分 + Round2的70分,勉强算是没有被 ...

  9. hdu 刷题记录

    1007 最近点对问题,采用分治法策略搞定 #include<iostream> #include<cmath> #include<algorithm> using ...

  10. js面向对象写页面

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