当前位置:网站首页>Leetcode interview question 17.21. water volume double pointer of histogram, monotonic stack /hard
Leetcode interview question 17.21. water volume double pointer of histogram, monotonic stack /hard
2022-07-27 15:27:00 【Abcheche】
List of articles
1.Description
Given a histogram ( Also known as histogram ), Suppose someone pours water from above , Finally, how much water can be stored in the histogram ? The width of the histogram is 1.
2.Example

Above is an array of [0,1,0,2,1,0,1,3,2,1,2,1] The histogram of representation , under these circumstances , Can connect 6 Units of water ( The blue part shows water ). thank Marcos Contribute to this map .
Input : [0,1,0,2,1,0,1,3,2,1,2,1]
Output : 6
3.Solution
1. Double pointer
Define two pointers to traverse from left and right sides to the middle , Save the highest point and add water during traversal .
When I wrote it myself, I didn't add the one with comments in the code if Conditions , Cause error , it is to be noted that .
Reference resources : Add link description
class Solution {
public static int trap(int[] height) {
int N = height.length;
if(N<2) {
return 0;
}
int left=0,right = N-1;
int lHeight = 0,rHeight = 0;
int res = 0;
while(left<right) {
// The condition here is : When the right side is higher than the left side, calculate the water volume on the left , When the left is higher than the right, calculate the water volume on the right ,
// That is, when calculating the water volume on the left, ensure that there must be a wall higher than the left on the right to form a pit with the left , Otherwise, the water volume cannot be calculated ; The same is true on the right .
if(height[left]<height[right]) {
if(height[left]<lHeight) {
res += lHeight - height[left];
}else {
lHeight = height[left];
}
left++;
}else {
if(height[right]<rHeight) {
res += rHeight - height[right];
}else {
rHeight = height[right];
}
right--;
}
}
return res;
}
}
2. Monotonic stack
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 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[(left > height(top]. If height[i]> height)top), Then you get an area that can receive rainwater , The width of this area is i- left-1, Height is min(height[left , heightji]) - height(top], According to the width and height, the amount of water that can be connected 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 water that can be connected ,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 i After calculating the amount of water that can be connected , take i Push , Continue to traverse the following subscripts , Calculate the amount of water that can be connected . After traversing, you can get the total amount of water that can be connected .
See the official explanation for the moving picture : Add link description
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;
}
}
边栏推荐
- STM32 can -- can ID filter analysis
- Network equipment hard core technology insider router Chapter 10 Cisco asr9900 disassembly (III)
- EMC design scheme of CAN bus
- generic paradigm
- 《剑指Offer》剪绳子
- EMC design scheme of RS485 interface
- 一些二进制位操作
- Wechat applet realizes music search page
- MOS管防止电源反接的原理
- STM32学习之CAN控制器简介
猜你喜欢
USB interface electromagnetic compatibility (EMC) solution

Do you really understand CMS garbage collector?

DIY ultra detailed tutorial on making oscilloscope: (1) I'm not trying to make an oscilloscope

Adaptation verification new occupation is coming! Huayun data participated in the preparation of the national vocational skill standard for information system adaptation verifiers

EMC design scheme of USB2.0 Interface

光电隔离电路设计方案(六款基于光耦、AD210AN的光电隔离电路图)

Unity mouse controls the first person camera perspective

ad7606与stm32连接电路介绍

Huayun data creates a perfect information technology and innovation talent training system to help the high-quality development of information technology and innovation industry

适配验证新职业来了!华云数据参与国家《信息系统适配验证师国家职业技能标准》编制
随机推荐
IJCAI 2022杰出论文公布,大陆作者中稿298篇拿下两项第一
Lua study notes
仪表放大器和运算放大器优缺点对比
Network equipment hard core technology insider router Chapter 11 Cisco asr9900 disassembly (V)
Unity最简洁的对象池实现
Network equipment hard core technology insider router Chapter 9 Cisco asr9900 disassembly (II)
Network equipment hard core technology insider router chapter Cisco asr9900 disassembly (I)
Data warehouse project is never a technical project
分布式锁
Leetcode 244 week competition - post competition supplementary question solution [broccoli players]
网络设备硬核技术内幕 路由器篇 20 DPDK (五)
JUC(JMM、Volatile)
反射
LeetCode 191. Number of 1 Bits(位1的个数) 位运算/easy
Discussion on STM32 power down reset PDR
多表查询_子查询概述和多表查询_子查询情况1&情况2&情况3
Reading notes of lifelong growth (I)
Usage of countdownlatch in multithreaded environment
Adaptation verification new occupation is coming! Huayun data participated in the preparation of the national vocational skill standard for information system adaptation verifiers
Digital storage oscilloscope based on FIFO idt7202-12