LeetCode 407. 接雨水 II (优先队列)
从最外圈开始不断向内遍历,如果内部的高度小于外部的高度,则证明该位置可以蓄水,否则不能,水会顺着该外圈流出去。
每次都处理外圈高度最小的那个位置 a,遍历它的四周。
如果它旁边的某个位置 b 高度小于 a,则证明 b 可以蓄水,因为 a 已经是四周最小的高度了,则 b 可以蓄水的大小就是 height(a)-height(b)
如果 b 的高度大于 a 则不能蓄水,水会顺着 a 流出去。
处理完 a 周围的所有位置后,把 a 删除,把 a 周围的位置当做新的边界。
其中,如果 a 周围的高度有小于 a 的,就补齐到 a,因为其是被 a 围住的,注水时高度不可能小 a。而如果高度大于 a 不用处理。
具体实现,先把最外圈的所有高度压入优先队列,然后每次取高度最小的去处理就可以了。
理解了还是觉得有点抽象。有一说一。其他题解一个都没看懂。
struct node {
int x, y, h;
node() {}
node(int x, int y, int h): x(x), y(y), h(h) {}
bool operator<(const node &rhs) const {
return h > rhs.h; // 保证优先队列是小顶堆
}
};
class Solution {
public:
int trapRainWater(vector<vector<int>>& heightMap) {
priority_queue<node> q;
int n = heightMap.size(), m = heightMap[0].size();
vector<vector<int>> visited(n, vector<int>(m, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i == 0 || j == 0 || i == n - 1 || j == m - 1) {
q.emplace(i, j, heightMap[i][j]);
visited[i][j] = 1;
}
}
}
int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int ans = 0;
while (q.size()) {
node cur = q.top(); q.pop();
for (int i = 0; i < 4; i++) {
int nx = cur.x + dir[i][0];
int ny = cur.y + dir[i][1];
if (nx < n && nx >= 0 && ny < m && ny >= 0 && !visited[nx][ny]) {
if (heightMap[nx][ny] < cur.h) {
ans += cur.h - heightMap[nx][ny];
q.emplace(nx, ny, cur.h);
} else {
q.emplace(nx, ny, heightMap[nx][ny]);
}
visited[nx][ny] = 1;
}
}
}
return ans;
}
};
LeetCode 407. 接雨水 II (优先队列)的更多相关文章
- Java实现 LeetCode 407 接雨水 II(二)
407. 接雨水 II 给定一个 m x n 的矩阵,其中的值均为正整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水. 说明: m 和 n 都是小于110的整数.每一个单位的高 ...
- Leetcode 407.接雨水
接雨水 给定一个 m x n 的矩阵,其中的值均为正整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水. 说明: m 和 n 都是小于110的整数.每一个单位的高度都大于0 且小 ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
- LeetCode:路径总和II【113】
LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
- LeetCode:接雨水【42】
LeetCode:接雨水[42] 题目描述 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1, ...
- [LeetCode] 407. Trapping Rain Water II 收集雨水 II
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- [LeetCode] 407. Trapping Rain Water II 收集雨水之二
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
随机推荐
- TypeScript快速上手
TypeScript快速上手 参考TypeScript零基础入门 轻松搞定ts进行整理 TS文档:TypeScript: The starting point for learning TypeScr ...
- 如何在AS中实现mysql查询并输出在视图上
新建子线程启用mysql new Thread(){ @override public void run(){ //在这里进行数据库调用 } }.start(); handler简单使用方法 hand ...
- systempath:Python开发者必备的文件与系统路径操作神器!
systempath - 专业级的文件与系统路径操作库 English | 中文 systempath 是一个专为Python开发者设计的,高度专业化的文件与系统路径操作库.通过提供一套直观且功能强大 ...
- 初看vue3源码
因为工作的原因又回到了vue的领域,需要加深对vue和vue生态的了解 也许平时比较多人手机看别人解析怎么看vue源码的,自己动手看vue源码的还是比较少,这次我想自己动手看看 首先 吧代码获取到本地 ...
- 【Linux】Re02
一.运行启动级别 0 关机 1 单用户 2 多用户状态没有网络服务 3 多用户状态存在网络服务 4 系统未使用保留给用户 5 图形界面 6 重启 命令: init [0 - 6] 图形化界面级别需要对 ...
- 【解决】ValueError: Memory growth cannot differ between GPU devices
在ubuntu系统下双显卡运行TensorFlow代码报错: ValueError: Memory growth cannot differ between GPU devices 报错的代码位置为: ...
- FIRD的防碰撞机制
RFID多标签阅读时防碰撞技术 概念 RFID 读写器在正常情况下一个时间点只能对磁场中的一张RFID卡进行读或写操作,但是实际应用中经常有当多张卡片同时进入读写器的射频场,读写器怎么处理呢?读写器需 ...
- 18-canvas绘制饼状图
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="U ...
- IDEA中try/catch快捷键
Ctrl + Alt + t
- 使用 Portainer CE 管理 Docker
此文档参考官方文档 Install Portainer CE with Docker on Linux 编写. 创建容器 docker volume create portainer_data 启动 ...