当前位置:网站首页>单调栈——42. 接雨水——面大厂必须会的困难题
单调栈——42. 接雨水——面大厂必须会的困难题
2022-07-28 03:21:00 【向着百万年薪努力的小赵】
1 题目描述
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
2 题目示例

输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
输出:6
解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。
示例 2:
输入:height = [4,2,0,3,2,5]
输出:9
3 题目提示
n == height.length
1 <= n <= 2 * 104
0 <= height[i] <= 105
4 思路
维护一个单调栈,单调栈存储的是下标,满足从栈底到栈顶的下标对应的数组 \textit{height}height 中的元素递减。
从左到右遍历数组,遍历到下标i时,如果栈内至少有两个元素,记栈顶元素为top,top 的下面一个元素是left,则一定有height[lefft]≥ height[top]。如果height[i]> height[top],则得到―个可以接雨水的区域,该区域的宽度是i- left - 1,高度是min( height[left , height[i]) — height[top],根据宽度和高度即可计算得到该区域能接的雨水量。
为了得到left,需要将top出栈。在对top计算能接的雨水量之后,left变成新的 top,重复上述操作,直到栈变为空,或者栈顶下标对应的height 中的元素大于或等于height[i]。
在对下标à处计算能接的雨水量之后,将主入栈,继续遍历后面的下标,计算能接的雨水量。遍历结束之后即可得到能接的雨水总量。
复杂度分析
时间复杂度:O(n),其中n是数组height的长度。从О到n —1的每个下标最多只会入栈和出栈各─次。
空间复杂度:O(n),其中n是数组height的长度。空间复杂度主要取决于栈空间,栈的大小不会超过n。
5 我的答案
class Solution {
public int trap(int[] height) {
int ans = 0;
Deque<Integer> stack = new LinkedList<Integer>();
int n = height.length;
for (int i = 0; i < n; ++i) {
while (!stack.isEmpty() && height[i] > height[stack.peek()]) {
int top = stack.pop();
if (stack.isEmpty()) {
break;
}
int left = stack.peek();
int currWidth = i - left - 1;
int currHeight = Math.min(height[left], height[i]) - height[top];
ans += currWidth * currHeight;
}
stack.push(i);
}
return ans;
}
}
边栏推荐
- Win11如何重命名音频设备
- How does win11 display fixed applications?
- MySQL的碎片有哪些
- 动态内存管理中的malloc、free、calloc、realloc动态内存开辟函数
- Unity简单实现对话功能
- xctf攻防世界 Web高手进阶区 unserialize3
- 20220726 at command test of Bluetooth module hc-05 of Huicheng Technology
- Illustrated: detailed explanation of JVM memory layout
- Methods of SQL server backup database
- [SAML SSO solution] Shanghai daoning brings you SAML for asp NET/SAML for ASP. Net core download, trial, tutorial
猜你喜欢

Win11黑色桌面背景如何解决?

收藏|0 基础开源数据可视化平台 FlyFish 大屏开发指南

The open source of "avoiding disease and avoiding medicine" will not go far

Methods of SQL server backup database

redis网络模型解析

Redis persistence mechanism

What if the word selection box of win11 input method is missing?

Response to questions about the balanced beacon group of Hubei University of Arts and Sciences

一篇文章掌握Postgresql中对于日期类数据的计算和处理

How to solve the problem of win11 black desktop background?
随机推荐
Win11怎么显示固定应用?
Redis内存回收
Summary of concurrent programming interview questions
2022-07-27:小红拿到了一个长度为N的数组arr,她准备只进行一次修改, 可以将数组中任意一个数arr[i],修改为不大于P的正数(修改后的数必须和原数不同), 并使得所有数之和为X的倍数。
Methods of SQL server backup database
沃尔沃:深入人心的“安全感” 究竟靠的是什么?
Outlook 教程,如何在 Outlook 中使用颜色类别和提醒?
Unity simply implements the dialog function
The wonderful use of asemi rectifier bridge GBPC3510 in DC brush motor
ssm整合(整合配置)
动画(animation)
⽇志分析⼯具(Splunk)
Digital twin technology drives smart factory to reduce burden and enhance operation and maintenance benefits
C WinForm development: how to add pictures to project resources
Redis persistence mechanism
GNU 通用公共许可证 v2.0 GNU GENERAL PUBLIC LICENSE
ThreadLocal usage scenario
max_ pool2d(): argument ‘input‘ (position 1) must be Tensor, not NoneType
Four methods of closing forms in C #
2022最新Android Handler相关面试题总结