当前位置:网站首页>407. 接雨水 II
407. 接雨水 II
2022-08-04 22:55:00 【小卢要刷力扣题】
前言
给你一个 m x n 的矩阵,其中的值均为非负整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水。
输入: heightMap = [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]]
输出: 4
解释: 下雨后,雨水将会被上图蓝色的方块中。总的接雨水量为1+2+1=4。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/trapping-rain-water-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路

如图,里面的水全部都能接住
最外层都放入小根堆内
弹出4,以4为瓶颈,如果里面旁边的数小于4,那证明能接住
如果大于4,那么水就会从缺口流出
弹出3,现在还是4为瓶颈,看看附近能接多少水
只要max不更新,都是max的可以接水的区域
max更新的情况:
弹出的元素比max大
代码
class Solution {
public static class Node{
public int value;
public int row;
public int col;
public Node(int v,int r,int c){
value=v;
row = r;
col = c;
}
}
public int trapRainWater(int[][] heightMap) {
if (heightMap == null || heightMap.length == 0 || heightMap[0] == null || heightMap[0].length == 0) {
return 0;
}
int n=heightMap.length;
int m=heightMap[0].length;
boolean[][] isEnter = new boolean[n][m];
PriorityQueue<Node> heap=new PriorityQueue<Node>((a,b)->a.value-b.value);
for(int i=1;i<n-1;i++){
heap.add(new Node(heightMap[i][0],i,0));
isEnter[i][0]=true;
heap.add(new Node(heightMap[i][m-1],i,m-1));
isEnter[i][m-1]=true;
}
for(int i=0;i<m;i++){
heap.add(new Node(heightMap[0][i],0,i));
isEnter[0][i]=true;
heap.add(new Node(heightMap[n-1][i],n-1,i));
isEnter[n-1][i]=true;
}
int water=0;
int max=0;
while(!heap.isEmpty()){
Node node=heap.poll();
max=Math.max(max,node.value);
int r=node.row;
int c=node.col;
if(r>0&&!isEnter[r-1][c]){
isEnter[r-1][c]=true;
water+=Math.max(0,max-heightMap[r-1][c]);
heap.add(new Node(heightMap[r-1][c],r-1,c));
}
if(r<n-1&&!isEnter[r+1][c]){
isEnter[r+1][c]=true;
water+=Math.max(0,max-heightMap[r+1][c]);
heap.add(new Node(heightMap[r+1][c],r+1,c));
}
if(c>0&&!isEnter[r][c-1]){
isEnter[r][c-1]=true;
water+=Math.max(0,max-heightMap[r][c-1]);
heap.add(new Node(heightMap[r][c-1],r,c-1));
}
if(c<m-1&&!isEnter[r][c+1]){
isEnter[r][c+1]=true;
water+=Math.max(0,max-heightMap[r][c+1]);
heap.add(new Node(heightMap[r][c+1],r,c+1));
}
}
return water;
}
}
边栏推荐
- Based on the results of the facts
- 现在学习次世代3D游戏建模还能找到高薪好工作吗
- Latex快速插入作者ORCID
- VC bmp文件总结
- Use ngrok to optimize web pages on raspberry pi (1)
- panic: reflect: reflect.Value.SetString using value obtained using unexported field
- 亿流量大考(3):不加机器,如何抗住每天百亿级高并发流量?
- enumerate()函数
- TypeScript - the use of closure functions
- 软件测试技术之如何编写测试用例(4)
猜你喜欢

go语言的日志实现(打印日志、日志写入文件、日志切割)

视频gif如何制作?试试这个视频制作gif神器

Pytest学习-Fixture

【3D建模制作技巧分享】ZBrush如何设置笔刷快捷键
![[Cultivation of internal skills of memory operation functions] memcpy + memmove + memcmp + memset (4)](/img/08/e115e1b0d801fcebef440ad4932610.png)
[Cultivation of internal skills of memory operation functions] memcpy + memmove + memcmp + memset (4)

轮播图动态渲染

未来我们还需要浏览器吗?(feat. 枫言枫语)

Reconfigure the ffmpeg plugin in chrome

生产者消费者问题

Using ngrok to optimize web pages on raspberry pi (2)
随机推荐
typeScript-闭包函数的使用
[Cultivation of internal skills of string functions] strlen + strstr + strtok + strerror (3)
go语言的日志实现(打印日志、日志写入文件、日志切割)
一点点读懂cpufreq(二)
SSM整合完整流程讲解
【游戏建模模型制作全流程】使用ZBrush制作骷髅王
js中小数四则运算精度问题原因及解决办法
一点点读懂regulator(四)
线上虚拟展馆展示具有哪些优势
PHP(3)
软件测试技术之如何编写测试用例(4)
剑指 Offer | 03. 数组中重复的数字
线性DP(下)
Shell expect real cases
One trick to cure pycharm DEBUG error UnicodeDecodeError: 'utf-8' codec can't decode
ANT1.7下载以及配置方法
Since a new byte of 20K came out, I have seen what the ceiling is
Both synchronized and ReentrantLock are smooth, because they are reentrant locks, and a thread will not deadlock if it takes the lock multiple times. We need reentrant locks
一点点读懂Thremal(二)
Based on the results of the facts