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

011. Container With Most Water[M]

问题

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.

思路

首先要明确一个我之前一直理解错的,它的题目是随意找2个木板构成木桶,容量最大,我之前以为是所有的木板已经在它的位置上了,然后找其中容量最大的——你加的水是可以漫过中间比较短的板子的。

如果按题意来,就简单了。

我们用两个指针来限定一个水桶,left,right。
h[i]表示i木板高度。
Vol_max表示木桶容量最大值

由于桶的容量由最短的那个木板决定:
Vol = min(h[left],h[right]) * (right - left)

  • left和right分别指向两端的木板。
  • left和right都向中央移动,每次移动left和Right中间高度较小的(因为反正都是移动一次,宽度肯定缩小1,这时候只能指望高度增加来增加容量,肯定是替换掉高度较小的,才有可能找到更大的容量。)
  • 看新桶子的容量是不是大于Vol_max,直到left和right相交。

代码如下:

public class Solution {
public int maxArea(int[] height) {
int l = 0,r = height.length-1;
int i = height[l] > height[r] ? r:l;
int vol,vol_max = height[i]*(r-l);
while(l < r)
{
if(height[l] < height[r]) l++;
else r--;
vol = Math.min(height[l],height[r]) * (r - l);
if(vol > vol_max) vol_max = vol;
}
return vol_max;
}
}

《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动的更多相关文章

  1. leetcode problem 11 Container With Most Water

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

  2. Leetcode Array 11 Container With Most Water

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

  3. leetcode个人题解——#11 Container with most water

    class Solution { public: int maxArea(vector<int>& height) { ; ; ; while(l < r) { int h ...

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

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

  5. 【LeetCode】11. Container With Most Water

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

  6. LeetCode OJ 11. Container With Most Water

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

  7. Leetcode题解之Container With Most Water

    1.题目描述 2.题目分析 首先,这个题可以使用暴力解法,时间复杂度是O(n^2),这个显然是最容易的做法,但是效率不够高,题目提供了一种解法,使用两个指针,一个从头向尾部,另外一个从尾部向头部,每一 ...

  8. 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 用双指针向中间滑动,较小的高度就作为当前情 ...

  9. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

随机推荐

  1. Modelsim10.2c使用教程(一个完整工程的仿真)

    之前玩过Altera的板子,不不, 现在应该叫intel PSG.在QuartusII13.0上老喜欢用modelsim_ae做仿真,小工程用起来也方便,但是我做IIC配置摄像头的时序仿真时,就显得有 ...

  2. java中父类的静态方法不能被重写

    Java中父类的静态方法确实不能被重写的,但是有的人可能去做实验发现在子类中去重写父类static方法时,并没什么问题.这里我来具体解释下. public class Parent { public ...

  3. 使用google chrome抓取数据:抓取全国的高中的数据

    http://tomycat.github.io/blog/other/2014/05/28/use-google-chrome-capture-data.html

  4. 在一般处理程序清理cookie

    清理cookie在ashx里面很奇怪,因为直接设置过期时间并不能成功,cookie还是会存在.所以需要添加一个同名的Cookie设置过期时间覆盖 HttpCookie cookie = null; / ...

  5. 利用backgroundwork----递归读取网页源代码,并下载href链接中的文件

    今天闲着没事,研究了一下在线更新程序版本的问题.也是工作中的需要,开始不知道如何下手,各种百度也没有找到自己想要的,因为我的需求比较简单,所以就自己琢磨了一下.讲讲我的需求吧.自己在IIs上发布了一个 ...

  6. Plasma Cash合约解读

    Plasma Cash合约解读 SmartPlasma 合约解读 1. 合约代码 2. 合约文件简单介绍 3. Plasma Cash 的基础数据结构 3.1 Plasma Cash 中的资产 3.2 ...

  7. 多个音频audio2

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

  8. “全栈2019”Java第一百零二章:哪些作用域可以声明局部内部类?

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  9. CAS客户端整合(二) Zabbix

    Zabbix是一个强大的服务器/交换机监控应用,有zabbix-server, zabbix-client, zabbix-web 三部分.zabbix-web管理端是用php写的. 前文参考:CAS ...

  10. linux centos 7 下安装ElasticSearch5.4

    一. 把elasticsearch-5.4.0.rpm和kibana-5.4.0-x86_64.rpm上传到centos下/root目录中,如下图:二.进入centos目录/root,并用命令rpm ...