407. 接雨水 II

简介: 文章目录前言解题思路代码前言给你一个 m x n 的矩阵,其中的值均为非负整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水。

文章目录

前言

解题思路

代码

前言

给你一个 m x n 的矩阵,其中的值均为非负整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水。


Interlaken Protocol白皮书(中文)

pdf


0星

超过10%的资源

851KB


下载

输入: 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;

   }

}


相关文章
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8068 2
|
11天前
|
域名解析 人工智能 Linux
|
21天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18526 64
|
19天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
5天前
|
人工智能 安全 网络安全
|
9天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1504 5
|
8天前
|
云安全 数据采集 人工智能
|
16天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2649 8
|
8天前
|
Web App开发 缓存 安全
巨魔商店 TrollStore 安装教程(2026):免电脑在线安装与全机型安装器矩阵_iOS巨魔安装
巨魔商店(TrollStore)是iOS 14–17.0(特定版本)的永久签名工具,无需越狱,利用CoreTrust漏洞实现IPA签名长期有效,彻底告别7天重签。支持免电脑在线安装(TrollHelperOTA),适配多机型/系统组合,开源免费,安全可控。(239字)
2135 0