当前位置: 首页 > news >正文

Leetcode-407. Trapping Rain Water II [C++][Java]

目录

一、题目描述

二、解题思路

【C++】

【Java】


Leetcode-407. Trapping Rain Water IIhttps://leetcode.com/problems/trapping-rain-water-ii/description/

一、题目描述

Given an m x n integer matrix heightMap representing the height of each unit cell in a 2D elevation map, return the volume of water it can trap after raining.

Example 1:

Input: heightMap = [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]]
Output: 4
Explanation: After the rain, water is trapped between the blocks.
We have two small ponds 1 and 3 units trapped.
The total volume of water trapped is 4.

Example 2:

Input: heightMap = [[3,3,3,3,3],[3,2,2,2,3],[3,2,1,2,3],[3,2,2,2,3],[3,3,3,3,3]]
Output: 10

Constraints:

  • m == heightMap.length
  • n == heightMap[i].length
  • 1 <= m, n <= 200
  • 0 <= heightMap[i][j] <= 2 * 104

二、解题思路

最小堆+BFS

  • 时间复杂度O(mnlog(mn))

  • 空间复杂度O(mn)

【C++】

class Solution {
private:int dirs[5] = {-1, 0, 1, 0, -1};
public:int trapRainWater(vector<vector<int>>& heightMap) {if (heightMap.size() < 3 || heightMap[0].size() < 3) {return 0;}int m = heightMap.size(), n = heightMap[0].size(), res = 0;priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;vector<vector<bool>> visit(m, vector<bool>(n, false));for (int i = 0; i < m; ++i) {for (int j = 0; j < n; ++j) {if (i == 0 || i == m - 1 || j == 0 || j == n - 1) {pq.push({heightMap[i][j], i * n + j});visit[i][j] = true;}}}while (!pq.empty()) {pair<int, int> cur = pq.top(); pq.pop();for (int k = 0; k < 4; ++k) {int nx = cur.second / n + dirs[k], ny = cur.second % n + dirs[k + 1];if (nx >= 0 && nx < m && ny >= 0 && ny < n && !visit[nx][ny]) {if (heightMap[nx][ny] < cur.first) {res += cur.first - heightMap[nx][ny];}visit[nx][ny] = true;pq.push({max(heightMap[nx][ny], cur.first), nx * n + ny});}}}return res;}
};

【Java】

class Solution {private int[] dirs = {-1, 0, 1, 0, -1};public int trapRainWater(int[][] heightMap) {if (heightMap == null || heightMap.length < 3 || heightMap[0].length < 3) {return 0;}int m = heightMap.length, n = heightMap[0].length, res = 0;PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]);boolean[][] visit = new boolean[m][n];for (int i = 0; i < m; ++i) {for (int j = 0; j < n; ++j) {if (i == 0 || i == m - 1 || j == 0 || j == n - 1) {pq.offer(new int[]{heightMap[i][j], i * n + j});visit[i][j] = true;}}}while (!pq.isEmpty()) {int[] cur = pq.poll();for (int k = 0; k < 4; ++k) {int nx = cur[1] / n + dirs[k], ny = cur[1] % n + dirs[k + 1];if (nx >= 0 && nx < m && ny >= 0 && ny < n && !visit[nx][ny]) {if (heightMap[nx][ny] < cur[0]) {res += cur[0] - heightMap[nx][ny];}visit[nx][ny] = true;pq.offer(new int[]{Math.max(heightMap[nx][ny], cur[0]), nx * n + ny});}}}return res;}
}

相关文章:

Leetcode-407. Trapping Rain Water II [C++][Java]

目录 一、题目描述 二、解题思路 【C】 【Java】 Leetcode-407. Trapping Rain Water IIhttps://leetcode.com/problems/trapping-rain-water-ii/description/ 一、题目描述 Given an m x n integer matrix heightMap representing the height of each unit cell in a 2D…...

详解 torch.triu:上三角矩阵的高效构造(中英双语)

详解 torch.triu&#xff1a;上三角矩阵的高效构造 在深度学习和矩阵运算中&#xff0c;我们经常需要构造上三角矩阵&#xff08;Upper Triangular Matrix&#xff09;&#xff0c;其中主对角线以下的元素全部设为 0。PyTorch 提供了一个高效的函数 torch.triu()&#xff0c;用…...

[ TypeScript ] “undefined extends xxx“ 总是为 true 的 bug

版本号 "typescript": "^5.7.3", "unplugin": "^2.2.0",说明 在使用 unplugin 时 , 我定义插件的参数是 必填的, 使用时却是一个可空参数, 不传参也不会报错, (options?: UserOptions) > Return &#x1f632;&#x1f632;&…...

高清下载油管视频到本地

下载工具并安装: yt-dlp官网地址&#xff1a; GitHub - yt-dlp/yt-dlp: A feature-rich command-line audio/video downloader ffmpeg官网地址&#xff1a; Download FFmpeg 注&#xff1a;记住为其添加环境变量 操作命令&#xff1a; 该指令表示以720p码率下载VIDEO_UR…...

Hadoop常用操作命令

在NameNode节点格式化集群 初始化集群 hdfs namenode -format启动HDFS sbin/start-dfs.sh启动yarn sbin/start-yarn.sh启动NodeManager yarn-daemon.sh start nodemanager启动DataNode hadoop-daemon.sh start datanode启动SecondaryNameNode hadoop-daemon.sh start se…...

[HOT 100] 2439. 最小化数组中的最大值

文章目录 1. 题目链接2. 题目描述3. 题目示例4. 解题思路5. 题解代码6. 复杂度分析 1. 题目链接 2439. 最小化数组中的最大值 - 力扣&#xff08;LeetCode&#xff09; 2. 题目描述 给你一个下标从 0 开始的数组 nums &#xff0c;它含有 n 个非负整数。 每一步操作中&#…...

【JavaEE进阶】图书管理系统 - 贰

目录 &#x1f332;前言 &#x1f384;设计数据库 &#x1f343;引⼊MyBatis和MySQL驱动依赖 &#x1f333;Model创建 &#x1f38d;约定前后端交互接口 &#x1f340;服务器代码 &#x1f6a9;控制层 &#x1f6a9;业务层 &#x1f6a9;数据层 &#x1f334;前端代码…...

Vue学习教程-14内置指令

文章目录 前言一、v-text指令二、v-html指令三、v-cloak指令四、v-once指令五、v-pre指令六、其他指令 前言 Vue.js 提供了许多内置指令&#xff08;Directives&#xff09;&#xff0c;这些指令用于在模板中添加特殊功能。内置指令以 v- 前缀开始。 v-text : 更新元素的 tex…...

【蓝桥杯单片机】客观题

一、第十三届省赛&#xff08;一&#xff09; 二、第十三届省赛&#xff08;二&#xff09;...

C++ 设计模式-访问者模式

C++访问者模式 一、模式痛点:当if-else成为维护噩梦 开发动物园管理系统,最初的需求很简单: class Animal {}; class Cat : public Animal {}; class Dog : public Animal {};// 处理动物叫声 void makeSound(Animal* a) {if (auto c = dynamic_cast<Cat*>(a)) {st…...

靶场之路-Kioptix Level-1 mod_ssl 缓冲区溢出漏洞

声明 学习视频来自B站UP主 泷羽sec,如涉及侵泷羽sec权马上删除文章笔记的只是方便各位师傅学习知识,以下网站涉及学习内容,其他的都与本人无关,切莫逾越法律红线,否则后果自负 一、准备工作 首先使用 vmware 导入靶机文件&#xff0c; 然后网络模式改成 nat 模式即可 我们打…...

【Viewer.js】vue3封装图片查看器

效果图 需求 点击图片放大可关闭放大的 图片 下载 cnpm in viewerjs状态管理方法 stores/imgSeeStore.js import { defineStore } from pinia export const imgSeeStore defineStore(imgSeeStore, {state: () > ({showImgSee: false,ImgUrl: ,}),getters: {},actions: {…...

stm32mp采用spi接口扩展can

在 STM32MP 系列微处理器中,通过 SPI 转 CAN 功能扩展 CAN 接口需要结合硬件设计(如使用 SPI 接口的 CAN 控制器芯片)和 Linux 驱动配置。以下是详细的实现步骤和关键点: 硬件选型与连接 常用 SPI 转 CAN 芯片MCP2515:经典 SPI 转 CAN 控制器,支持 CAN 2.0B。MCP2517FD:…...

forge-1.21.x模组开发(二)给物品添加功能

功能效果 创建一个兑换券&#xff0c;当使用兑换券对着兑换机右键时&#xff0c;获得一条烤鱼 创建兑换券 创建ExchangeCouponsItem.java&#xff0c;继承Item&#xff0c;定义兑换券内容 public class ExchangeCouponsItem extends Item {public ExchangeCouponsItem(Prop…...

创建第一个 Maven 项目(一)

一、引言 在 Java 开发的广袤天地中&#xff0c;Maven 宛如一位全能的管家&#xff0c;发挥着举足轻重的作用。它是一个基于项目对象模型&#xff08;POM&#xff09;的项目管理和构建自动化工具&#xff0c;极大地简化了 Java 项目的开发流程。 Maven 的核心优势之一在于其强…...

网络运维学习笔记 022 HCIA-Datacom新增知识点03园区网典型组网架构及案例实战

园区网典型组网架构及案例实战 园区网&#xff1a;内部运行了园区网协议的一个主体网络 园区网络典型架构 园区网络常用协议与技术&#xff1a; 接入层&#xff1a; VLAN、生成树、链路聚合、AAA、dhcp-snooping等 汇聚层&#xff1a;DHCP、堆叠、链路聚合、生成树、OSPF、静…...

python-leetcode-二叉树的直径

543. 二叉树的直径 - 力扣&#xff08;LeetCode&#xff09; # Definition for a binary tree node. # class TreeNode: # def __init__(self, val0, leftNone, rightNone): # self.val val # self.left left # self.right right class Solutio…...

ubuntu中打包与压缩命令详解

Ubuntu 中打包与压缩命令详解 在 Ubuntu 系统中&#xff0c;打包和压缩文件是常见的操作。通过打包和压缩&#xff0c;可以将多个文件或目录合并为一个文件&#xff0c;并减小文件大小以节省存储空间或方便传输。本文将详细介绍 Ubuntu 中常用的打包与压缩命令及其用法。 目录…...

Linux MySQL 8.0.29 忽略表名大小写配置

Linux MySQL 8.0.29 忽略表名大小写配置 问题背景解决方案遇到的问题&#xff1a; 问题背景 突然发现有个大写的表报不存在。 在Windows上&#xff0c;MySQL是默认支持忽略大小写的。 这个时候你要查询一下是不是没有配置&#xff1a; SHOW VARIABLES LIKE lower_case_table…...

【c++】【线程池】线程池模式

【c】【线程池】线程池模式 1 L/F领导者与跟随者模式 概述&#xff1a;在此模式中&#xff0c;线程池中的线程分为&#xff1a;领导者&#xff08;Leader&#xff09;&#xff0c;跟随者&#xff08;Follower&#xff09;和工作者&#xff08;Processor&#xff09; 领导者线…...

Linux 文件类型,目录与路径,文件与目录管理

文件类型 后面的字符表示文件类型标志 普通文件&#xff1a;-&#xff08;纯文本文件&#xff0c;二进制文件&#xff0c;数据格式文件&#xff09; 如文本文件、图片、程序文件等。 目录文件&#xff1a;d&#xff08;directory&#xff09; 用来存放其他文件或子目录。 设备…...

Appium+python自动化(十六)- ADB命令

简介 Android 调试桥(adb)是多种用途的工具&#xff0c;该工具可以帮助你你管理设备或模拟器 的状态。 adb ( Android Debug Bridge)是一个通用命令行工具&#xff0c;其允许您与模拟器实例或连接的 Android 设备进行通信。它可为各种设备操作提供便利&#xff0c;如安装和调试…...

【Linux】C语言执行shell指令

在C语言中执行Shell指令 在C语言中&#xff0c;有几种方法可以执行Shell指令&#xff1a; 1. 使用system()函数 这是最简单的方法&#xff0c;包含在stdlib.h头文件中&#xff1a; #include <stdlib.h>int main() {system("ls -l"); // 执行ls -l命令retu…...

python/java环境配置

环境变量放一起 python&#xff1a; 1.首先下载Python Python下载地址&#xff1a;Download Python | Python.org downloads ---windows -- 64 2.安装Python 下面两个&#xff0c;然后自定义&#xff0c;全选 可以把前4个选上 3.环境配置 1&#xff09;搜高级系统设置 2…...

Linux相关概念和易错知识点(42)(TCP的连接管理、可靠性、面临复杂网络的处理)

目录 1.TCP的连接管理机制&#xff08;1&#xff09;三次握手①握手过程②对握手过程的理解 &#xff08;2&#xff09;四次挥手&#xff08;3&#xff09;握手和挥手的触发&#xff08;4&#xff09;状态切换①挥手过程中状态的切换②握手过程中状态的切换 2.TCP的可靠性&…...

微信小程序 - 手机震动

一、界面 <button type"primary" bindtap"shortVibrate">短震动</button> <button type"primary" bindtap"longVibrate">长震动</button> 二、js逻辑代码 注&#xff1a;文档 https://developers.weixin.qq…...

Psychopy音频的使用

Psychopy音频的使用 本文主要解决以下问题&#xff1a; 指定音频引擎与设备&#xff1b;播放音频文件 本文所使用的环境&#xff1a; Python3.10 numpy2.2.6 psychopy2025.1.1 psychtoolbox3.0.19.14 一、音频配置 Psychopy文档链接为Sound - for audio playback — Psy…...

【开发技术】.Net使用FFmpeg视频特定帧上绘制内容

目录 一、目的 二、解决方案 2.1 什么是FFmpeg 2.2 FFmpeg主要功能 2.3 使用Xabe.FFmpeg调用FFmpeg功能 2.4 使用 FFmpeg 的 drawbox 滤镜来绘制 ROI 三、总结 一、目的 当前市场上有很多目标检测智能识别的相关算法&#xff0c;当前调用一个医疗行业的AI识别算法后返回…...

AspectJ 在 Android 中的完整使用指南

一、环境配置&#xff08;Gradle 7.0 适配&#xff09; 1. 项目级 build.gradle // 注意&#xff1a;沪江插件已停更&#xff0c;推荐官方兼容方案 buildscript {dependencies {classpath org.aspectj:aspectjtools:1.9.9.1 // AspectJ 工具} } 2. 模块级 build.gradle plu…...

初探Service服务发现机制

1.Service简介 Service是将运行在一组Pod上的应用程序发布为网络服务的抽象方法。 主要功能&#xff1a;服务发现和负载均衡。 Service类型的包括ClusterIP类型、NodePort类型、LoadBalancer类型、ExternalName类型 2.Endpoints简介 Endpoints是一种Kubernetes资源&#xf…...