当前位置:网站首页>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;
}
}
边栏推荐
- 【3D建模制作技巧分享】ZBrush如何重新拓扑
- 【字符串函数内功修炼】strlen + strstr + strtok + strerror(三)
- Based on the results of the facts
- Go 编程语言(简介)
- 「津津乐道播客」#397 厂长来了:怎样用科技给法律赋能?
- 【游戏建模模型制作全流程】使用ZBrush制作骷髅王
- ffplay视频播放原理分析
- [QNX Hypervisor 2.2用户手册]10.4 vdev hpet
- truffle
- [Cultivation of internal skills of string functions] strncpy + strncat + strncmp (2)
猜你喜欢
【3D建模制作技巧分享】在zbrush中如何雕刻头发 ZBrush头发雕刻小技巧
【游戏建模模型制作全流程】在ZBrush中雕刻恶魔城男性角色模型
SRv6网络的安全解决方案
测试薪资这么高?刚毕业20K,仅需3.5个月
【3D建模制作技巧分享】ZBrush纹理贴图怎么导入
最温馨的家园
【论文笔记KDD2021】MixGCF: An Improved Training Method for Graph Neural Network-based Recommender Systems
使用cpolar优化树莓派上的网页(2)
【3D建模制作技巧分享】ZBrush模型如何添加不同材质
2022年全网最全接口自动化测试框架搭建,没有之一
随机推荐
[Cultivation of internal skills of string functions] strncpy + strncat + strncmp (2)
线上虚拟展馆展示具有哪些优势
3D建模师为了让甲方爸爸过稿,还可以这么做,就是在赚血汗钱啊
祝福一路顺风
Jbpm3.2 开发HelloWorld (简单请假流程)客户端
基于内容的图像检索系统设计与实现--颜色信息--纹理信息--形状信息--PHASH--SHFT特征点的综合检测项目,包含简易版与完整版的源码及数据!
synchronized和ReentrantLock都很丝滑,因为他们都是可重入锁,一个线程多次拿锁也不会死锁,我们需要可重入
线性DP(下)
被领导拒绝涨薪申请,跳槽后怒涨9.5K,这是我的心路历程
kernel hung_task死锁检测机制原理实现
go语言的time包介绍
C5750X7R2E105K230KA(电容器)MSP430F5249IRGCR微控制器资料
【2020】【论文笔记】超表面:多功能和可编程——
生成回文数
One trick to cure pycharm DEBUG error UnicodeDecodeError: 'utf-8' codec can't decode
最温馨的家园
使用cpolar优化树莓派上的网页(2)
重新配置chrome中ffmpeg插件
BUG | 接口返回异常数据
Latex fast insert author ORCID