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). ...
随机推荐
- 【hiho一下 第十一周】树中的最长路
[题目链接]:http://hihocoder.com/problemset/problem/1050 [题意] [题解] 有一个经典的求树的直径的方法; 首先; 树的直径的两端的端点必然都在树的叶子 ...
- 工具-docker01
- 洛谷 1775. [国家集训队2010]小Z的袜子
1775. [国家集训队2010]小Z的袜子 ★★★ 输入文件:hose.in 输出文件:hose.out 简单对比时间限制:1 s 内存限制:512 MB [题目描述] 作为一个生活 ...
- byte类型取值范围以及溢出具体解释
例1: public class test { public static void main(String[] args) { byte a = 127 ; a = (byte)(a+3) ; Sy ...
- HDU 5372 Segment Game
/** 多校联合2015-muti7-1004 <a target=_blank href="http://acm.hdu.edu.cn/showproblem.php?pid=537 ...
- linux虚拟机网络设置(本机使用wiff,自己的网)
一.linux虚拟机网络设置(https://jingyan.baidu.com/album/4e5b3e1957979d91901e24f1.html?picindex=16) 选中虚拟机,点击 ...
- 剑指offer面试题14(Java版):调整数组顺序使奇数位于偶数的前面
题目:输入一个整数数组.实现一个函数来调整该数组中数字的顺序.使得全部奇数位于数组的前半部分.全部偶数位于数组的后半部分. 1.基本实现: 假设不考虑时间复杂度,最简单的思路应该是从头扫描这个数组,每 ...
- #定位系统性能瓶颈# perf
perf是一个基于Linux 2.6+的调优工具,在liunx性能測量抽象出一套适应于各种不同CPU硬件的通用測量方法,其数据来源于比較新的linux内核提供的 perf_event 接口 系统事件: ...
- 【opencv】opencv在vs下的配置(持续更新)
经常使用配置记录,会更新下去. 1.去掉ipch及.sdf文件 opencv在vs编译会得到很多文件.当中.dsf和ipch文件就有几十M.总是非常占空间,而这都是用来保存C++预编译的头文件和Int ...
- ios-UI-汤姆猫德游戏实现
// // ViewController.m // UI-猜拳游戏 // // Created by jzq_mac on 15/7/15. // Copyright (c) 2015年 jz ...