新闻详情

新闻详情

首页 / 资讯中心 / 详情

364 · 接雨水 II (Heap)

发布时间:2026/9/18 13:14:18来源:尧图网络
364 · 接雨水 II (Heap)
。链接九章算法 - 帮助更多程序员找到好工作硅谷顶尖IT企业工程师实时在线授课为你传授面试技巧题解九章算法 - 帮助更多程序员找到好工作硅谷顶尖IT企业工程师实时在线授课为你传授面试技巧九章算法强化班1.从四周向里面看将四周元素放入堆中用堆获得一个最低洼的位置2.用这个低洼的位置向四周看如果max(当前位置-四周位置的height0)存入结果3.同时把四周位置填平放入到堆中标记访问过visitedclass Solution { public: struct Node { int x; int y; int height; bool operator(const Node node) const { return height node.height ? true : false; } }; int trapRainWater(vectorvectorint heightMap) { int m heightMap.size(); if (m 0) { return 0; } int n heightMap[0].size(); if (n 0) { return 0; } priority_queueNode que; unordered_setint visited; for (int i 0; i m; i) { for (auto j : {0, n - 1}) { int xy_node i * n j; if (visited.count(xy_node) 0) { Node node; node.x i; node.y j; node.height heightMap[i][j]; visited.insert(xy_node); que.push(node); } } } for (int j 0; j n; j) { for (auto i : {0, m-1}) { int xy_node i * n j; if (visited.count(xy_node) 0) { Node node; node.x i; node.y j; node.height heightMap[i][j]; visited.insert(xy_node); que.push(node); } } } int result 0; vectorvectorint direc{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; while (!que.empty()) { auto f que.top(); que.pop(); for (auto d : direc) { int next_x f.x d[0]; int next_y f.y d[1]; if (next_x 0 || next_y 0 || next_x m || next_y n) { continue; } int next_xy_node next_x * n next_y; if (visited.count(next_xy_node) ! 0) { continue; } visited.insert(next_xy_node); Node node; node.x next_x; node.y next_y; node.height max(f.height, heightMap[next_x][next_y]); result f.height heightMap[next_x][next_y] ? f.height - heightMap[next_x][next_y] : 0; que.push(node); } } return result; } };O(nm n*mlog(nm))typedef pairint,int pii; class Solution { public: int trapRainWater(vectorvectorint heightMap) { if (heightMap.size() 2 || heightMap[0].size() 2) { return 0; } int m heightMap.size(); int n heightMap[0].size(); priority_queuepii, vectorpii, greaterpii pq; vectorvectorbool visit(m, vectorbool(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; } } } int res 0; int dirs[] {-1, 0, 1, 0, -1}; while (!pq.empty()) { pii curr pq.top(); pq.pop(); for (int k 0; k 4; k) { int nx curr.second / n dirs[k]; int ny curr.second % n dirs[k 1]; if( nx 0 nx m ny 0 ny n !visit[nx][ny]) { if (heightMap[nx][ny] curr.first) { res curr.first - heightMap[nx][ny]; } visit[nx][ny] true; pq.push({max(heightMap[nx][ny], curr.first), nx * n ny}); } } } return res; } };
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

DeepSeek R1实战:从参数调优到API部署与结构化输出 2026/9/18 13:59:24

DeepSeek R1实战:从参数调优到API部署与结构化输出

简介:面向DeepSeek R1新手与进阶用户的实战技巧合集,围绕模型获取途径、提示词写法、创意玩法和场景化提问展开,帮助读者避开服务器拥堵与信息碎片化,快速上手并提升输出质量。资源共包含1个PDF文件,压缩包约530KB&…

阅读更多 →
Gyroflow|用陀螺仪数据把手持抖动的视频拉回稳定 2026/9/18 13:59:24

Gyroflow|用陀螺仪数据把手持抖动的视频拉回稳定

Gyroflow|用陀螺仪数据把手持抖动的视频拉回稳定 【免费下载链接】gyroflow Video stabilization using gyroscope data 项目地址: https://gitcode.com/GitHub_Trending/gy/gyroflow 用 GoPro 贴着雪道俯冲拍完,回放时画面晃得根本没法看。Gyrof…

阅读更多 →
cann-perf-ui-json-report:CANN oam-tools 交互式 UI JSON 性能报告的生成、校验与交付 2026/9/18 13:59:24

cann-perf-ui-json-report:CANN oam-tools 交互式 UI JSON 性能报告的生成、校验与交付

cann-perf-ui-json-report:CANN oam-tools 交互式 UI JSON 性能报告的生成、校验与交付 【免费下载链接】oam-tools 本项目为开发者提供故障定位工具,包含故障信息收集,软硬件信息展示,AI core error报错分析等能力,提…

阅读更多 →
Hugo 文件缓存配置完全指南:caches 参数详解、令牌机制与垃圾回收 2026/9/18 13:59:24

Hugo 文件缓存配置完全指南:caches 参数详解、令牌机制与垃圾回收

Hugo 文件缓存配置完全指南:caches 参数详解、令牌机制与垃圾回收 【免费下载链接】hugo The world’s fastest framework for building websites. 项目地址: https://gitcode.com/gh_mirrors/hu/hugo Hugo 通过文件缓存(file cache)将…

阅读更多 →
实战查看QQ好友IP地址:Wireshark抓包与Nginx日志 2026/9/18 13:59:24

实战查看QQ好友IP地址:Wireshark抓包与Nginx日志

简介:一份关于查看QQ好友IP地址的实操型文档,围绕这一特定需求整理了两类可行做法:使用显IP补丁和使用绿色版显IP插件。内容适合对聊天数据显示方式感兴趣、希望了解QQ好友IP查看原理的普通用户,也可作为网络工具类文档的写作参考…

阅读更多 →
基于本地缓存的 fallback 降级机制:Hystrix 服务降级的原理与实战 2026/9/18 13:56:23

基于本地缓存的 fallback 降级机制:Hystrix 服务降级的原理与实战

基于本地缓存的 fallback 降级机制:Hystrix 服务降级的原理与实战 【免费下载链接】advanced-java 😮 Core Interview Questions & Answers For Experienced Java(Backend) Developers | 互联网 Java 工程师进阶知识完全扫盲:涵盖高并发、…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞