题意

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.

思路

假如有容器如下图:

那么他装满水之后一定是这个样子:

可以看出,其从中间到两边一定是呈阶梯状下降的,中间的空缺一定会被水填上的。所以我们只需要枚举上图中由蓝色组成的矩形即可。

开始做的时候竟然不会做了。然后问了下学妹,说是单调队列,发现不会写…… 真是忧伤。

代码

public class Solution {
public int maxArea(int[] height) {
int l = 0;
int r = height.length - 1;
int rlt = calcArea(l, r, height);
while (l < r) {
if (height[l] < height[r]) {
l = nextLeftBoard(l, r, height);
} else {
r = nextRightBoard(l, r, height);
}
rlt = Math.max(calcArea(l, r, height), rlt);
}
return rlt;
} private int nextLeftBoard(int l, int r, int[] height) {
int rlt = l;
while (rlt < r && height[rlt] <= height[l])
rlt ++;
return rlt;
} private int nextRightBoard(int l, int r, int[] height) {
int rlt = r;
while (l < rlt && height[rlt] <= height[r])
rlt --;
return rlt;
} private int calcArea(int l, int r, int[] height) {
int h = Math.min(height[l], height[r]);
return (r-l) * h;
}
}

LeetCode 11. Container With Most Water 单调队列的更多相关文章

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

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

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

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

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

  4. LeetCode 11. Container With Most Water (装最多水的容器)

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

  5. [LeetCode] 11. Container With Most Water 装最多水的容器

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

  6. LeetCode#11. Container With Most Water

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

  7. Java [leetcode 11] Container With Most Water

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

  8. C#解leetcode 11. Container With Most Water

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

  9. [LeetCode] 11. Container With Most Water My Submissions Question 解题思路

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

随机推荐

  1. qt4.7.0 交叉编译环境搭建经验总结

    一.前期软件准备: 1 .虚拟机fedora9.到fedora官网下载,地址 http://fedoraproject.org/   版本推荐使用fedora9,在vm内安装,并且不安装vmware ...

  2. SecureRandom生成随机数超慢 导致tomcat启动时间过长的解决办法

    用腾讯云的CentOS 7.2 CVM 服务器跑Tomcat时发现,Tomcat启动的特别慢,通过查看日志,发现时间主要花在实例化SecureRandom对象上了. 由该日志可以看出,实例化该对象使用 ...

  3. IntelliJ IDEA 对于generated source的处理

    IntelliJ IDEA 对于generated source的处理 学习了:https://stackoverflow.com/questions/5170620/unable-to-use-in ...

  4. linux下測试硬盘读写速度

    买了个ssd硬盘,就想着跟普通的机械盘做个比較.由于桌面装的是ubuntu系统,所以就想用linux的命令简单測一下好了 以下是ssd的性能数据: 測试写: xxx@WaitFish:~ > t ...

  5. 转:IOS远程推送通知

    在ios系统中,app应用程序无法在后台完成较多的任务,仅仅允许程序做一些有限的任务(如音视频播放.地理位置信息.voip).然而,如果你想做 一些有趣的事情,并且告知用户,甚至用户没有使用你的app ...

  6. robot framework框架selenium API

    RIDE面板认识 selenium API 关键字 语法 参数 备注 Open Browser url Chrome 用不同的浏览器打开url,需要下载不同的浏览器驱动,默认火狐 Close Brow ...

  7. Android 对话框(Dialog) 及 自己定义Dialog

    Activities提供了一种方便管理的创建.保存.回复的对话框机制,比如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...

  8. c3---scanf

    #include <stdio.h> int main(int argc, const char * argv[]) { // 要求: 存储用户输入的整数 // 1.用户输入的整数确定吗? ...

  9. Adding Kentico controls to the Visual Studio toolbox

    https://docs.kentico.com/k10/references/kentico-controls https://docs.kentico.com/k10/references/ken ...

  10. 利用机器学习进行DNS隐蔽通道检测——数据收集,利用iodine进行DNS隐蔽通道样本收集

    我们在使用机器学习做DNS隐蔽通道检测的过程中,不得不面临样本收集的问题,没办法,机器学习没有样本真是“巧妇难为无米之炊”啊! 本文简单介绍了DNS隐蔽通道传输工具iodine,并介绍如何从iodin ...