参考 优先队列的思路解决接雨水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 (优先队列)的更多相关文章

  1. Java实现 LeetCode 407 接雨水 II(二)

    407. 接雨水 II 给定一个 m x n 的矩阵,其中的值均为正整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水. 说明: m 和 n 都是小于110的整数.每一个单位的高 ...

  2. Leetcode 407.接雨水

    接雨水 给定一个 m x n 的矩阵,其中的值均为正整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水. 说明: m 和 n 都是小于110的整数.每一个单位的高度都大于0 且小 ...

  3. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  4. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  5. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

  6. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

  7. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

  8. LeetCode:接雨水【42】

    LeetCode:接雨水[42] 题目描述 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1, ...

  9. [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 ...

  10. [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 ...

随机推荐

  1. SpringBoot实战:Spring Boot接入Security权限认证服务

    引言 Spring Security 是一个功能强大且高度可定制的身份验证和访问控制的框架,提供了完善的认证机制和方法级的授权功能,是一个非常优秀的权限管理框架.其核心是一组过滤器链,不同的功能经由不 ...

  2. pytest批量执行多个测试文件(批量执行一个文件夹下的所有测试用例)

    图片 代码 #!/usr/bin/env python # @File : test_runall.py import pytest import os # path = os.path.dirnam ...

  3. 【Tutorial C】08 函数 Function

    函数的定义 C源程序是由函数组成的. 最简单的程序有一个主函数 main(),但实用程序往往由多个函数组成, 由主函数调用其他函数,其他函数也可以互相调用. 函数是C源程序的基本模块,程序的许多功能是 ...

  4. 【SpringBoot】13 数据访问P1 整合Jdbc

    SpringBoot与数据访问概述: 对于数据访问层,无论是SQL还是NOSQL,Spring Boot默认采用整合Spring Data的方式进行统一处理, 添加大量自动配置,屏蔽了很多设置.引入各 ...

  5. 从.net开发做到云原生运维(七)——服务网格Istio

    1. 前言 上篇文章我们讲了dapr,dapr作为开发阶段使用的组件,需要开发人员知道怎么使用,到此篇文章之前,开发人员的开发任务已经完成了,剩下的就是一些运维相关的事情了.假设我们的服务已经开发完成 ...

  6. 从.net开发做到云原生运维(三)——.net core的学习路线

    1. 前言 前面几篇文章主要讲了.net技术相关的基本概念和一些涉及的项目什么,本身也没讲太多底层的技术,这篇文章依旧不讲具体的技术,主要介绍.net技术的学习路线.学习路线也是我18年毕业的时候看到 ...

  7. 始智AI —— https://wisemodel.cn/ —— 试用

    清华大学的合资企业推出的服务: 始智AI -- https://wisemodel.cn/ 链接: 始智AI -- https://wisemodel.cn/ 和modelscope比相对简约,毕竟功 ...

  8. mpi4py和cupy的联合应用(anaconda环境):GPU-aware MPI + Python GPU arrays

    Demo代码: from mpi4py import MPI import cupy as cp comm = MPI.COMM_WORLD size = comm.Get_size() rank = ...

  9. C# 导出表格时表头优化思路

    众所周知 众所周知,如果使用DataTable.一般的思路是这么写的 var exprotData = new DataTable("Datas"); exprotData.Col ...

  10. docker 常用工具

    windows 下常常需要linux环境 直接安装虚拟机不方便也浪费资源 所以直接在docker下安装一个centos 然后搭建好开发环境就是个不错的办法 一.Linux 环境 1.安装centos ...