我现在在做一个叫《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. ZOJ3700 Ever Dream 2017-04-06 23:22 76人阅读 评论(0) 收藏

    Ever Dream Time Limit: 2 Seconds      Memory Limit: 65536 KB  "Ever Dream" played by Nigh ...

  2. MFC中的一般经验之谈4

    MFC中的窗口控件,都是从CWnd类中继承的.MFC以及ResourceEditor支持的所有控件称为标准控件. 在对话框资源文件夹下,然后邮件新建添加新对话框,新建对话框后要在资源视图中的对话框文件 ...

  3. memory leak-----tomcat日志warn

    web应用借助于结构:spring mvc + quartz结构,部署到tomcat容器时,shutdown时的error信息: appears to have started a thread na ...

  4. EAS_AOP分布式事务

    在System.Transactions事务体系中,为事务提供了7种不同的隔离级别.这7中隔离级别分别通过 System.Transactions.IsolationLevel的7个枚举项表示. pu ...

  5. Replication--复制相关的作业

    复制使用下列作业来执行计划维护和按需维护 作业名称 说明 默认调度 代理历史记录清除:分发 从分发数据库中删除复制代理历史记录. 每十分钟运行一次 分发清除:分发 从分发数据库中删除复制的事务. 停用 ...

  6. shell中调用jenkins API批量运行历史任务

    shell中调用jenkins API批量运行jenkins带参数的任务: #!/bin/sh #startdate=20150127 startdate=20150201 while [ " ...

  7. 【Oracle 12c】最新CUUG OCP-071考试题库(56题)

    56.(14-14) choose the best answer: You need to create a table with the following column specificatio ...

  8. 域名可以解析(ping域名可以获取正确ip),服务器本地telnet 域名+端口 无法连接,通过建立本地虚拟域名指定的方法解决该问题

    环境: 服务器A,网管已为A开通外网ip,且设置有映射域名:假如内网ip为172.16.2.6.外网ip为123.123.123.123.域名为test.sstest.com 现象: 服务器A,tel ...

  9. git命令合集及github的克隆推送

    安装git 初始化仓库 提交相关 撤销相关 远程推送 分支相关 其他 遇到的错误 github的克隆上传 此文章只是对命令的一个统计,起备忘和复习git只是的作用,不建议从没接触过git的同学通过它来 ...

  10. Spring aop+自定义注解统一记录用户行为日志

    写在前面 本文不涉及过多的Spring aop基本概念以及基本用法介绍,以实际场景使用为主. 场景 我们通常有这样一个需求:打印后台接口请求的具体参数,打印接口请求的最终响应结果,以及记录哪个用户在什 ...