题意

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. 计算机网络系统--常用DOS命令

    01.名称:md 用法:md “文件夹名” 用处:批量建立文件夹 02.关机命令 shutdown At 18:00 shutdown –s      18:00关机 shutdown -s -t 3 ...

  2. HDU 4686

    再不能直视这道题,换INT64就过了....... 同样可以使用矩阵的方法.构造1*5的 D[N],a[n],b[n],a[n]*b[n],1 接着你应该就会了. #include <iostr ...

  3. HDU 1171 Big Event in HDU(多重背包)

    Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...

  4. 深刻理解Nginx之Nginx完整安装

    1.   Nginx安装 1.1预先准备 CentOS系统下,安装Nginx的库包依赖. 安装命令例如以下: sudo yum groupinstall "DevelopmentTools& ...

  5. shell文本过滤编程(十一):paste命令

    [版权声明:转载请保留出处:blog.csdn.net/gentleliu. Mail:shallnew at 163 dot com] 从字面上能够看出.paste命令和cut命令功能相反,cut命 ...

  6. 【C语言】编写函数实现字符串旋转

    //编写函数实现字符串旋转 #include <stdio.h> #include <assert.h> #include <string.h> void reve ...

  7. ubuntu14.04无法安装Curl

    ubuntu14.04无法安装Curl apt-get install curl 提示没有这个软件 源 更换软件源到163也不行,更新软件源也不行. 解决:參考http://www.linuxidc. ...

  8. c5

    // // main.c // Switch练习2 // // Created by xiaomage on 15/6/6. // Copyright (c) 2015年 xiaomage. All ...

  9. php面向对象之get和set方法

    php面向对象之get和set方法 简介 1.自己写get或者set 2.用系统的魔术方法__get和__set 代码 <?php class Person{ private $userName ...

  10. c++面向对象程序设计 课后题 答案 谭浩强 第四章

    c++面向对象程序设计课后题答案 谭浩强 第四章 1: #include <iostream> using namespace std; class Complex {public: Co ...