当前位置:网站首页>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;
}
}
边栏推荐
- DirectX 入门知识
- 如何帮助企业优化Office管理
- What is the execution method of the stand-alone parallel query of PostgreSQL?
- Hdu3117 Fibonacci numbers [mathematics]
- Printf function buffer problem
- The database uses PSQL and JDBC to connect remotely and disconnect automatically from time to time
- Database storage series (1) column storage
- CPU、GPU、NPU的区别
- What if win11 wallpaper turns black? The solution of win11 wallpaper blackening
- mysql保存数据提示:Out of range value for column错误
猜你喜欢

Thread knowledge summary
![[ManageEngine] what is Siem](/img/a6/0fbe60df6bef337a91a10fe046aa8a.jpg)
[ManageEngine] what is Siem

DirectX 入门知识

Shell programming specifications and variables

Redis

Airport cloud business sign analysis
![[popular science] the difference and connection between accuracy and resolution](/img/12/efcce1f6b8801d8d8b08b79818632c.png)
[popular science] the difference and connection between accuracy and resolution

如何做好企业系统漏洞评估

Toward fast, flexible, and robust low light image enhancement cvpr2022

Docker practical experience: deploy mysql8 master-slave replication on docker
随机推荐
深圳市人力资源和社会保障局关于发放脱贫人口就业有关补贴的通知
Import the virtual machine officially made by Kali Linux into Oracle VirtualBox
lc marathon 7.26
【医疗行业】DICOM converter Tools
Stm32f103c8t6 drives sh1106 1.3 "IIC OLED display under Arduino frame
Txt replace line breaks with spaces or cancel line breaks
【WORK】关于技术架构
微信小程序实现音乐搜索页面
va_ List usage summary
Automatically configure SSH password free login and cancel SSH password free configuration script
在Oracle VirtualBox中导入Kali Linux官方制作的虚拟机
自动化配置SSH免密登录和取消SSH免密配置脚本
Spark job uses log4j appender to append logs to local files or mysql
Database storage series (1) column storage
Forward proxy and reverse proxy
视觉系统设计实例(halcon-winform)-10.PLC通讯
Who can't capture packets these days? Wireshark packet capture and common protocol analysis are for you!
网络设备硬核技术内幕 路由器篇 21 可重构的路由器
图解 SQL,这也太形象了吧
如何做好企业系统漏洞评估