当前位置:网站首页>Monotonous stack -- 42. Receiving rain -- a difficult problem that big factories must know
Monotonous stack -- 42. Receiving rain -- a difficult problem that big factories must know
2022-07-28 03:45:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
Given n Nonnegative integers indicate that each width is 1 Height map of columns of , Calculate columns arranged in this way , How much rain can be received after rain .
2 Title Example

Input :height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output :6
explain : Above is an array of [0,1,0,2,1,0,1,3,2,1,2,1] Height map of representation , under these circumstances , Can connect 6 Units of rain ( The blue part indicates rain ).
Example 2:
Input :height = [4,2,0,3,2,5]
Output :9
3 Topic tips
n == height.length
1 <= n <= 2 * 104
0 <= height[i] <= 105
4 Ideas
Maintain a monotone stack , The monotone stack stores subscripts , Satisfy the array corresponding to the subscript from the bottom of the stack to the top of the stack \textit{height}height Decreasing elements in .
Traversing the array from left to right , Traverse to subscript i when , If there are at least two elements in the stack , The top element of the stack is top,top The next element of is left, There must be height[lefft]≥ height[top]. If height[i]> height[top], Then we get ― An area that can receive rainwater , The width of this area is i- left - 1, Height is min( height[left , height[i]) — height[top], According to the width and height, the amount of rainwater that can be received in this area can be calculated .
In order to get left, Need to put top Out of the stack . In the face of top After calculating the amount of rain that can be received ,left Become new top, Repeat the above operation , Until the stack becomes empty , Or the subscript at the top of the stack corresponds to height The element in is greater than or equal to height[i].
On the subscript à After calculating the amount of rain that can be connected , Put the main stack , Continue to traverse the following subscripts , Calculate the amount of rain that can be received . After traversing, you can get the total amount of rainwater that can be received .
Complexity analysis
Time complexity :O(n), among n It's an array height The length of . from О To n —1 At most, each subscript of will only be put into and out of the stack ─ Time .
Spatial complexity :O(n), among n It's an array height The length of . The space complexity mainly depends on the stack space , The stack size will not exceed n.
5 My answer
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;
}
}
边栏推荐
- [wrong question]mocha and railgun
- Msgan is used for pattern search of multiple image synthesis to generate confrontation Network -- to solve the problem of pattern collapse
- 玩一玩WolframAlpha计算知识引擎
- Server memory failure prediction can actually do this!
- 动态规划——509. 斐波那契数
- ES6 from entry to mastery 07: Deconstruction assignment
- 高等数学(第七版)同济大学 习题3-5 个人解答
- 高等数学(第七版)同济大学 习题3-6 个人解答
- TypeError: ufunc ‘bitwise_and‘ not supported for the input types, and the inputs could not be safely
- Golang gets the tag of the loop nested structure
猜你喜欢

【论文笔记】基于深度学习的移动机器人自主导航实验平台

构建“产业大脑”,以“数字化”提升园区运营管理及服务能力!

MySQL Basics (create, manage, add, delete, and modify tables)

Selenium--WEB自动化测试工具

ES6 从入门到精通 # 07:解构赋值

WordPress simple mkblog blog theme template v2.1

Advanced Mathematics (Seventh Edition) Tongji University exercises 3-4 personal solutions (first 8 questions)

Airiot Q & A issue 6 | how to use the secondary development engine?

AI首席架构师12-AICA-百度OCR垂类规模化落地实践

Qt:qmessagebox message box, custom signal and slot
随机推荐
Data rich Computing: m.2 meets AI at the edge
贪心——45. 跳跃游戏 II
Volvo: what on earth does the deep-rooted "sense of security" rely on?
2022-07-27: Xiao Hong got an array arr with a length of N. she is going to modify it only once. She can modify any number arr[i] in the array to a positive number not greater than P (the modified numb
Vertical align align the elements in the row are vertically centered
Ch340 RTS DTR pin programming drives OLED
LeetCode_409_最长回文串
Interface automation test, complete introduction
动态规划——62. 不同路径
Input upload file and echo FileReader and restrict the type of file selection
《剑指offer》| 刷题小记
Swift中的协议
递归和非递归分别实现求第n个斐波那契数
Differences among BRD, MRD and PRD
[openvx] VX for basic use of objects_ matrix
Weekly recommended short video: how to correctly understand the word "lean"?
LeetCode 0141. 环形链表 - 三种方法解决
高等数学(第七版)同济大学 习题3-4 个人解答(前8题)
静态博客搭建工具汇总
After 95, Alibaba P7 published the payroll: it's really heartbreaking