LeetCode 11. Container With Most Water 单调队列
题意
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 单调队列的更多相关文章
- 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 用双指针向中间滑动,较小的高度就作为当前情 ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode] 11. Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- LeetCode#11. Container With Most Water
问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- Java [leetcode 11] Container With Most Water
问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- C#解leetcode 11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [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). ...
随机推荐
- 【codeforces 799B】T-shirt buying
[题目链接]:http://codeforces.com/contest/799/problem/B [题意] 告诉你每个人喜欢的衣服的颜色; 然后告诉你每件衣服的正面和背面的颜色以及它的价格; 只要 ...
- [Ionic] Align and Size Text with Ionic CSS Utilities
The Ionic framework provides several built-in CSS Utilities or directives that you can leverage when ...
- Linux系统编程——进程替换:exec 函数族
在 Windows 平台下,我们能够通过双击运行可运行程序,让这个可运行程序成为一个进程.而在 Linux 平台.我们能够通过 ./ 运行,让一个可运行程序成为一个进程. 可是.假设我们本来就执行着一 ...
- node-webkit 屏幕截图功能
做 IM 屏幕截图是少不了的,之前 windows 版本是调用的 qq 输入法的截图功能,这个版本又再次尝试自己实现发现是可以的,getusermedia 的权限很高,代码如下 <!DOCTYP ...
- poj 2528 Mayor's posters 【线段树 + 离散化】
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 50643 Accepted: 14675 ...
- unity3D游戏开发实战原创视频讲座系列9之塔防类游戏开发第一季
解说文件夹 塔防游戏0基础篇... 第一讲 游戏演示和资源介绍... 第二讲 游戏场景的完毕... 第三讲 预制体的制作... 第四讲 敌人的随机产生和按路径行走... 第五讲 塔防工具的产 ...
- 2015.05.04,外语,读书笔记-《Word Power Made Easy》 14 “如何谈论日常现象” SESSION 41
1. people are the craziest animals bovine(['bәuvain] adj. (似)牛的, 迟钝的),像牛一样placid(['plæsid] adj. 安静的, ...
- TS4
类: 类与对象字面量和接口差不多,比较两个类类型的对象时,只有实例的成员会被比较. 静态成员和构造函数不在比较的范围内. class Animal { feet: number; constructo ...
- python中类的定义、实例化、封装以及私有变量/方法
1. 定义类 python中定义一个类的格式如下: class MyClass(object): def __init__(self,data1,data2): self.__data1=data1 ...
- Caused by: java.lang.NoClassDefFoundError: org/apache/neethi/AssertionBuilderFactory
转自:https://blog.csdn.net/iteye_8264/article/details/82641058 1.错误描述 严重: StandardWrapper.Throwable or ...