当前位置:网站首页>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;
}
}
边栏推荐
- Is it safe to open an account on a mobile phone?
- Code coverage statistical artifact -jacobo tool practice
- 《剑指Offer》两个链表的第一个公共结点
- How to package AssetBundle
- Simple mathematical knowledge related to 3D
- 同花顺开户在手机开户安全吗?
- Discussion on STM32 power down reset PDR
- 多线程环境下CountDownLatch的用法
- 光电隔离电路设计方案(六款基于光耦、AD210AN的光电隔离电路图)
- 网络设备硬核技术内幕 路由器篇 5 汤普金森漫游网络世界(上)
猜你喜欢

Unity performance optimization ----- occlusion culling of rendering optimization (GPU)

西瓜书《机器学习》阅读笔记之第一章绪论

After configuring corswebfilter in grain mall, an error is reported: resource sharing error:multiplealloworiginvalues

LeetCode 240. 搜索二维矩阵 II medium

反射

EMC design scheme of USB2.0 Interface

generic paradigm

Code coverage statistical artifact -jacobo tool practice

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

华云数据打造完善的信创人才培养体系 助力信创产业高质量发展
随机推荐
多表查询_子查询概述和多表查询_子查询情况1&情况2&情况3
基于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
Several basic uses of tl431-2.5v voltage reference chip
LeetCode 190. 颠倒二进制位 位运算/easy
STM32F10x_ Hardware I2C read / write EEPROM (standard peripheral library version)
Network equipment hard core technology insider router Chapter 7 tompkinson roaming the network world (Part 2)
网络设备硬核技术内幕 路由器篇 3 贾宝玉梦游太虚幻境 (中)
两阶段提交与三阶段提交
"Sword finger offer" linked list inversion
After configuring corswebfilter in grain mall, an error is reported: resource sharing error:multiplealloworiginvalues
Kubernetes CNI classification / operation mechanism
LeetCode 781. 森林中的兔子 哈希表/数学问题 medium
CAN总线的EMC设计方案
Leetcode 244 week competition - post competition supplementary question solution [broccoli players]
2022-07-27 Daily: IJCAI 2022 outstanding papers were published, and 298 Chinese mainland authors won the first place in two items
Wechat applet realizes music search page
LeetCode 191. Number of 1 Bits(位1的个数) 位运算/easy
EMC design scheme of RS485 interface
JMeter recording interface automation