当前位置:网站首页>LeetCode 面试题 17.21. 直方图的水量 双指针,单调栈/hard
LeetCode 面试题 17.21. 直方图的水量 双指针,单调栈/hard
2022-07-27 14:05:00 【押切徹】
1.Description
给定一个直方图(也称柱状图),假设有人从上面源源不断地倒水,最后直方图能存多少水量?直方图的宽度为 1。
2.Example

上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的直方图,在这种情况下,可以接 6 个单位的水(蓝色部分表示水)。 感谢 Marcos 贡献此图。
输入: [0,1,0,2,1,0,1,3,2,1,2,1]
输出: 6
3.Solution
1.双指针
定义两个指针分别从左右两边往中间遍历,遍历的过程中保存最高点并且加上水量。
当时自己写的时候没有加上代码中加注释的那一个if条件,导致错误,要注意。
参考:添加链接描述
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) {
//这里的条件是:当右边比左边高时计算左边的水量,左边比右边高时计算右边的水量,
//即计算左边的水量时保证右边一定有一个比左边高的墙能与左边形成一个坑,否则将无法计算水量;右边也是同理。
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.单调栈
维护一个单调栈,单调栈存储的是下标,满足从栈底到栈顶的下标对应的数组height 中的元素递减。
从左到右遍历数组,遍历到下标i时,如果栈内至少有两个元素,记栈顶元素为top,top的下面一个元素是left,则一定有height[(left > height(top]。如果height[i]> height)top),则得到一个可以接雨水的区域,该区域的宽度是i- left-1,高度是min(height[left , heightji]) - height(top],根据宽度和高度即可计算得到该区域能接的水的量。
为了得到left,需要将top出栈。在对 top计算能接的水的量之后,left变成新的top,重复上述操作,直到栈变为空,或者栈顶下标对应的height中的元素大于或等于height[i]。
在对下标i处计算能接的水的量之后,将i入栈,继续遍历后面的下标,计算能接的水的量。遍历结束之后即可得到能接的水的总量。
动图见官方题解:添加链接描述
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;
}
}
边栏推荐
- 在Oracle VirtualBox中导入Kali Linux官方制作的虚拟机
- Timestamp of AAC, h264, etc
- 【云享读书会第13期】视频文件的封装格式
- Redis
- UnityUI方面处理(归纳与积累)
- [Yunxiang book club issue 13] multimedia processing tool ffmpeg tool set
- What if win11 wallpaper turns black? The solution of win11 wallpaper blackening
- Nokia's patent business was hit for the first time, and Chinese enterprises are not so easy to knead
- JS什么是声明提前?函数与变量声明提前的先后顺序(执行上下文铺垫篇)
- CPU、GPU、NPU的区别
猜你喜欢

Finally, someone finished all the dynamic planning, linked list, binary tree and string required for the interview

How to do well in enterprise system vulnerability assessment
Database storage series (1) column storage

Lecture 4: Longest ascending substring

软件产品第三方测试费用为什么没有统一的报价?

Summary of basic knowledge of C language

Who can't capture packets these days? Wireshark packet capture and common protocol analysis are for you!

股票买卖4

Graphic SQL of giant image

视觉系统设计实例(halcon-winform)-9.文字显示
随机推荐
Navicate reports an error access violation at address 00000000
南山区民政局关于开展2022年度南山区社会组织等级评估工作的通知
User question understanding and answer content organization for epidemic disease Science Popularization
NEFU117 素数个数的位数【素数定理】
JS 疫情宅在家,学习不能停,七千字长文助你彻底弄懂原型与原型链
Redis
C language layered understanding (C language array)
Toward Fast, Flexible, and Robust Low-Light Image Enhancement(实现快速、灵活和稳健的弱光图像增强)CVPR2022
Skywalking distributed system application performance monitoring tool - medium
什么是Tor?Tor浏览器更新有什么用?
Research on Chinese idiom metaphorical knowledge recognition and relevance based on transfer learning and text enhancement
Why is there no unified quotation for third-party testing fees of software products?
数据仓库项目从来不是技术项目
web上构建3d效果 基于three.js的实例
如果我们是那晚负责修复 B 站崩了的开发人员
Txt replace line breaks with spaces or cancel line breaks
Understand JS execution context in an article
在Oracle VirtualBox中导入Kali Linux官方制作的虚拟机
aac 和 h264等的时间戳
Construction and empirical research of post talent demand analysis framework based on recruitment advertisement