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 ;
}

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

  1. 【LeetCode】011 Container With Most Water

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

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

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

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

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

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

  5. No.011 Container With Most Water

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

  6. 【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). ...

  7. [Leetcode]011. Container With Most Water

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

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

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

  9. 【LeetCode】11. Container With Most Water 盛最多水的容器

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...

随机推荐

  1. 浅析AnyCast网络技术

    什么是BGP AnyCast? BGP anycast就是利用一个(多个) as号码在不同的地区广播相同的一个ip段.利用bgp的寻路原则,短的as path 会选成最优路径(bgp寻路原则之n),从 ...

  2. [Java核心技术]第四章-对象与类(4.1-4.6总结)

    4.1面向对象程序设计概述 OOP(面向对象编程Object Oriented Programming) OOP中数据第一位,算法第二位. 类 封装:关键在于不能让其他方法直接访问类的实例域,程序仅通 ...

  3. Extension-valuepart

    data: ls_extension type bapiparex. loop at extensionin into ls_extension . read table extensionout i ...

  4. linux/windows转mac的习惯设置

    外接键盘 常用快捷键设置 中英文快捷键名都重新设置一遍 使用ctrl替代command(对 内置 键盘操作) https://jingyan.baidu.com/article/6f2f55a1465 ...

  5. C# 检测证书是否安装、 安装证书

    检测是否存在指定的证书: /// <summary> /// 检测是否存在指定的证书 /// </summary> /// <returns></return ...

  6. osg探究补充:Node::accept(NodeVisitor& nv)及NodeVisitor简介

    前言 在前几节中,我自己觉得讲的比较粗糙,因为实在是时间上不是很充足,今天我想弥补一下,希望不是亡羊补牢.我们在osgViewer::Viewer::eventTraversal()函数中经常看到这么 ...

  7. 现代编译原理--第六章(中间树 IR Tree 含源码)

    (转载请表明出处   http://www.cnblogs.com/BlackWalnut/p/4559717.html ) 这一章,就虎书而言,理论知识点是及其少的,就介绍了为什么要有一个中间表示树 ...

  8. 设计模式 策略模式2 c++11

    根据需求的不同 选择不同的策略算法 之前是保存的各种策略类的指针 这里直接使用 function  bind 选择对应的算法 代码 // 005.cpp: 定义控制台应用程序的入口点. // #inc ...

  9. SPARK安装二:HADOOP集群部署

    一.hadoop下载 使用2.7.6版本,因为公司生产环境是这个版本 cd /opt wget http://mirrors.hust.edu.cn/apache/hadoop/common/hado ...

  10. QT中报错collect2:ld returned 1 exit status的可能原因。

    参考:https://blog.csdn.net/u014546553/article/details/78781547 1.编译成功的例子在后台执行,有时一闪而过,如果再次build ,则会提示上述 ...