二维数组存储的是每个单元的高度,求最多接多少体积的雨水

首先想到的就是找到最低点

注意看后两个图,当水超过3的时候,可以从绿的的点流进内部

所以可以想象内部现在的水的高度可以达到3,减去原来的体积,就是水的体积

太机智了吧

这个就是想象,想象水流进来的感觉,从内部往外面扩展,找不到什么规律

要注意的一点就是,四周的点是没什么用的,因为首先积水肯定不会在四周

然后内部有积水后,将数组中的高度重新赋值为当前积水高度

public class Solution {public class Cell {int row;int col;int height;public Cell(int row, int col, int height) {this.row = row;this.col = col;this.height = height;}}public int trapRainWater(int[][] heights) {if (heights == null || heights.length == 0 || heights[0].length == 0)return 0;PriorityQueue<Cell> queue = new PriorityQueue<>(1, new Comparator<Cell>(){public int compare(Cell a, Cell b) {return a.height - b.height;}});int m = heights.length;int n = heights[0].length;boolean[][] visited = new boolean[m][n];// Initially, add all the Cells which are on borders to the queue.for (int i = 0; i < m; i++) {visited[i][0] = true;visited[i][n - 1] = true;queue.offer(new Cell(i, 0, heights[i][0]));queue.offer(new Cell(i, n - 1, heights[i][n - 1]));}for (int i = 0; i < n; i++) {visited[0][i] = true;visited[m - 1][i] = true;queue.offer(new Cell(0, i, heights[0][i]));queue.offer(new Cell(m - 1, i, heights[m - 1][i]));}// from the borders, pick the shortest cell visited and check its neighbors:// if the neighbor is shorter, collect the water it can trap and update its height as its height plus the water trapped// add all its neighbors to the queue.int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};int res = 0;while (!queue.isEmpty()) {Cell cell = queue.poll();for (int[] dir : dirs) {int row = cell.row + dir[0];int col = cell.col + dir[1];if (row >= 0 && row < m && col >= 0 && col < n && !visited[row][col]) {visited[row][col] = true;res += Math.max(0, cell.height - heights[row][col]);queue.offer(new Cell(row, col, Math.max(heights[row][col], cell.height)));}}}return res;}
}

太难了,所以看得网上大神做的复制过来

大神厉害,自定义一个cell结构

创建具有 PriorityQueue初始容量的PriorityQueue,根据指定的比较器对其元素进行排序。

值得注意的是,优先级队列的默认排序是数值大的优先级高,排在前面,所以我们需要重写排序函数

leetcode 407 收集雨水(java 搜索)相关推荐

  1. [Leetcode][第35题][JAVA][搜索插入位置][二分法]

    [问题描述][中等] [解答思路] 二分法 时间复杂度:O(LogN) 空间复杂度:O(1) public class Solution {public int searchInsert(int[] ...

  2. Leetcode 407. Trapping Rain Water II 收集雨水2 解题报告

    1 解题思想 我看了下题目,发现比预想中的简单,加之我比较烂,所以其实我还是没做,只是看懂了上回贴的代码,然后做了一下注释,现在我来讲下题目. 首先请看下上一题,上一题是2D的这题是3D的: Leet ...

  3. LeetCode 2086. 从房屋收集雨水需要的最少水桶数(贪心)

    文章目录 1. 题目 2. 解题 1. 题目 给你一个下标从 0 开始的字符串 street .street 中每个字符要么是表示房屋的 'H' ,要么是表示空位的 '.' . 你可以在 空位 放置水 ...

  4. LeetCode 42 Trapping Rain Water 收集雨水

    LeetCode 42 Trapping Rain Water 收集雨水 Given n non-negative integers representing an elevation map whe ...

  5. Leetcode中二叉树中的搜索相关题目解析以及java实现

    Leetcode中二叉树中的搜索相关题目解析以及java实现 这一类的题目其实稍微有一些杂,基本就是在二叉树中寻找一些或者某个特定值,题目比较多,我们会通过两道三篇来总结,不过总体来说也基本上就是BF ...

  6. [Leetcode][第79题][JAVA][单词搜索][DFS][回溯]

    [问题描述][中等] [解答思路] 1. DFS繁琐版本 class Solution {public boolean exist(char[][] board, String word) {bool ...

  7. leetcode 从房屋收集雨水需要的最少水桶数

    给你一个下标从 0 开始的字符串 street .street 中每个字符要么是表示房屋的 'H' ,要么是表示空位的 '.' . 你可以在 空位 放置水桶,从相邻的房屋收集雨水.位置在 i - 1  ...

  8. LeetCode 2086. 从房屋收集雨水需要的最少水桶数 -- 动态规划

    从房屋收集雨水需要的最少水桶数 中等 26 相关企业 给你一个下标从 0 开始的字符串 street .street 中每个字符要么是表示房屋的 'H' ,要么是表示空位的 '.' . 你可以在 空位 ...

  9. apache flume_Flume:使用Apache Flume收集客户产品搜索点击数据

    apache flume 这篇文章涵盖了使用Apache flume收集客户产品搜索点击并使用hadoop和elasticsearch接收器存储信息. 数据可能包含不同的产品搜索事件,例如基于不同方面 ...

最新文章

  1. hdoj1423 最长上升公共子序列
  2. JavaScript数据运算
  3. 利用BeanUtils在对象间复制属性
  4. 51Nod1556 计算
  5. windows mysql 自动备份的几种方法
  6. #pragma pack(push,1) #pragma pack(pop)
  7. 前端学习(1743):前端调试值之调试元素的hover样式
  8. php将字符变为数字,数字字符怎么转化为数字 php 怎么将字符转成数字
  9. mybatis返回map键值对_mybatis返回map key怎么指定
  10. 微博feed系统的push和pull模式和时间分区拉模式架构探讨
  11. linux 停止jar 指令,Linux编辑启动、停止与重启springboot jar包脚本实例
  12. Rootkit Hunter系统检测工具
  13. c++前置声明的一点总结
  14. java代码获取数据源_tomcat中获取不同的数据源java代码
  15. tbschedule源码学习
  16. Docker搭建Jenkins2.60.3版本,及更换jenkins下载源
  17. 读取Unique reads
  18. CUDA out of memory. Tried to allocate 150.00 MiB (GPU 0; 4.00 GiB total capacity; 2.24 GiB already a
  19. 正襟危坐说--操作系统(伍):进程间通信
  20. OSChina 周一乱弹 —— 这些年经历了不少爱情历险

热门文章

  1. 遗传算法(Genetic algorithm,GA)前世今生(python实现)
  2. 【python turtle画图】太极图绘制实例
  3. 软件说明|Google地球影像下载及导航软件无法使用的解决办法
  4. lr背景虚化_lr背景虚化_怎样拍出背景模糊的照片
  5. 《python 网络爬虫技术》参考答案 第1章~第7章
  6. YOLOv7训练自己的数据集(口罩检测)
  7. 【进阶C语言】通讯录(完整版)
  8. labview与stm32通信
  9. PTA7-6混合类型数据格式化输入
  10. 添加Path有什么用?