11. Container With Most Water

  • Total Accepted: 86363
  • Total Submissions: 244589
  • Difficulty: Medium

  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.(不能倾斜容器)

思路:

  这道题很简单,大概意思是要找到两条条纵线然后这两条线以及X轴构成的容器能容纳最多的水。而任意两条线与x轴一起能装的水的最大量就是两条线在x轴的距离乘以较矮的那条线的高度即可。所以解题思路就很简单了。

方法一:

  暴力求法,求每一种情况下的最大装水面积,然后比较是否最大,不是则下一种,是则替换为当前最大值。

 public int maxArea2(int[] height) {
int n = height.length ;
if(height == null || n < 2){
return 0 ;
}
int max = 0 ;
for(int i = 0 ; i < n ; i++){
for(int j = i+1 ; j < n ; j++){
int low = height[i]<height[j]?height[i]:height[j] ;
int area = low*(j-i)/2 ;
max = max>area?max:area ;
}
}
return max ;
}

方法二:

  最大盛水量取决于两边中较短的那条边,而且如果将较短的边换为更短边的话,盛水量只会变少。所以我们可以用两个头尾指针,计算出当前最大的盛水量后,将较短的边向中间移,因为我们想看看能不能把较短的边换长一点。这样一直计算到左边大于右边为止就行了。

  注意:如果将较短的边向中间移后,新的边还更短一些,其实可以跳过,减少一些计算量

 public int maxArea(int[] height) {

     int n = height.length ;
if(height == null || n < 2){
return 0 ;
}
int max = 0 ;
int left = 0 ;
int right = n-1 ;
while(left < right){
int low = height[left]<height[right]?height[left]:height[right] ;
int area = low*(right-left) ;
max = max>area?max:area ;
if(low == height[left]){
left++ ;
}else{
right-- ;
}
}
return max ;
}

No.011 Container With Most Water的更多相关文章

  1. LeetCode--No.011 Container With Most Water

    11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...

  2. 【JAVA、C++】LeetCode 011 Container With Most Water

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

  3. 【LeetCode】011 Container With Most Water

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

  4. [Leetcode]011. Container With Most Water

    public class Solution { public int maxArea(int[] height) { int left = 0, right = height.length - 1; ...

  5. 011 Container With Most Water 盛最多水的容器

    给定 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) .画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线,使得它们 ...

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

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

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

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

  8. [LintCode] Container With Most Water 装最多水的容器

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

  9. 67. Container With Most Water

    Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...

随机推荐

  1. 删除指定的文件.bat

    @echo offattrib -s -h -r /s /d C:\*Thumbs.dbattrib -s -h -r /s /d D:\*Thumbs.dbattrib -s -h -r /s /d ...

  2. Css3_写出小广播样子

    /* create an arrow that points up */ div.arrow-up { width:0px; height:0px; border-left:5px solid tra ...

  3. Linux进程内存分析pmap命令(转)

    名称:       pmap - report memory map of a process(查看进程的内存映像信息)用法       pmap [ -x | -d ] [ -q ] pids... ...

  4. (WPF) 文件和文件夹选择对话框。

    点击button,选择一个excel文件,并将文件名显示在textbox上. private void btnSelectErrorTableFile_Click(object sender, Rou ...

  5. C# 通过委托控制进度条以及多线程更新控件

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. linux命令(13) 删除指定文件夹下后缀名相同的文件

    方法一: find 目录 -name "*.abc" | xargs rm命令有点危险,可以先执行前半段,看看是不是你要删除的文件, 然后再整条执行 方法二:find . -nam ...

  7. LPC1768之中断

    一外中断:只有特定的4个外中断引脚, 1 在特定的引脚上,引脚功能要选对应的外中断功能 2设定的触发条件(高低电平.上升/下降沿) 3 NVIC设定,特定的通道. 二GPIO中断: 1只有GPIO0和 ...

  8. 树莓派3上安装Qt5

    按照在2上的安装只安装了qt4,实际上qt5已经可以直接apt方式可以获取到树莓派上了. install qt5-default and qtcreator:$ sudo apt-get instal ...

  9. OOP三个基本特征:封装、继承、多态

    面向对象的三个基本特征是:封装.继承.多态. 封装 封装最好理解了.封装是面向对象的特征之一,是对象和类概念的主要特性. 封装,也就是把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类 ...

  10. CSS如何实现数字分页效果

    代码实例如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...