接雨水

给定一个 m x n 的矩阵,其中的值均为正整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水。

说明:

都是小于110的整数。每一个单位的高度都大于0 且小于 20000。

示例:

给出如下 3x6 的高度图:

[

[1,4,3,1,3,2],

[3,2,1,3,2,4],

[2,3,3,2,3,1]

]

返回 4。

如上图所示,这是下雨前的高度图[[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]] 的状态。

下雨后,雨水将会被存储在这些方块中。总的接雨水量是4。

三维的装水的问题,主要思路还是从边缘的地方开始找。

priorityqueue每次poll出最高优先级的元素,也就是目前height最底的元素。

对于visit之后的元素来说,新的高度是要updata的,要么保持原样,要么变成装了水之后的高度。

 import java.util.Comparator;
import java.util.PriorityQueue; public class Solution { private static class Cell {
private int row;
private int col;
private int height; public Cell(int row, int col, int height){
this.row = row;
this.col = col;
this.height = height;
}
} public static int trapRainWater(int[][] heightMap) {
if(heightMap == null || heightMap.length == 0 ||heightMap[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 = heightMap.length;
int n = heightMap[0].length;
boolean[][] visited = new boolean[m][n]; for (int i = 0; i< m ;i++){
visited[i][0] = true;
visited[i][n-1] = true;
queue.offer(new Cell(i, 0, heightMap[i][0]));
queue.offer(new Cell(i, n-1, heightMap[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, heightMap[0][i]));
queue.offer(new Cell(m-1, i, heightMap[m-1][i]));
} 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 - heightMap[row][col]);
queue.offer(new Cell(row,col,Math.max(heightMap[row][col],cell.height)));
}
}
}
return res;
} public static void main(String[] args){
int[][] heightMap={{1,4,3,1,3,2},{3,2,1,3,2,4},{2,3,3,2,3,1}};
trapRainWater(heightMap);
}
}
																																																																																																																																																																																																	int[][] heightMap={{1,4,3,1,3,2},{3,2,1,3,2,4},{2,3,3,2,3,1}};
trapRainWater(heightMap); }}

Leetcode 407.接雨水的更多相关文章

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

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

  2. LeetCode:接雨水【42】

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

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

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

  5. [LeetCode]42. 接雨水(双指针,DP)

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

  6. [leetcode] 407. Trapping Rain Water II

    https://leetcode.com/contest/6/problems/trapping-rain-water-ii/ 看到这题,我很高兴,因为我做过!哈哈!其实我现在也写不出来,知道大概思想 ...

  7. leetcode 42. 接雨水 JAVA

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

  8. Leetcode 42.接雨水

    接雨水 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下 ...

  9. Leetcode 42 接雨水 双指针

    地址 https://leetcode-cn.com/problems/trapping-rain-water/ 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能 ...

随机推荐

  1. springJDBC 事物隔离

    五.Spring-jdbc的实现 第一步:导jar包 pom.xml <!--引入spring-beans节点--><dependency>    <groupId> ...

  2. AJAX的JSON方式传回方法

    AJAX返回数据的类型有两种,一种是TEXT类型,一种是JSON类型. 使用TEXT类型,访问数据库后将结果拼接成字符串,返回时在拆分成数组使用. JSON直接将结果转成JSON数据,返回时直接使用. ...

  3. 微信公众号与HTML 5混合模式揭秘5——JSSDK开发技巧1

    微信公众号与HTML 5混合模式揭秘1——如何部署JSSDK 微信公众号与HTML 5混合模式揭秘2——分享手机相册中照片 微信公众号与HTML 5混合模式揭秘3——JSSDK获取地理位置 微信公众号 ...

  4. The Singapore NRIC Check Digit

    The Singapore NRIC number is made up of 7 digits and a letter behind. This letter is calculated from ...

  5. (十三)maven之release和snapshots

    发布release 用户A将代码打包到RELEASE仓库.用户B使用时,需要在pom.xml添加jar包的依赖坐标.如果用户A将jar包版本从1.0升级到2.0,用户B使用时也需要在pom.xml中修 ...

  6. 状态压缩---状态压缩dp第一题

    标签: ACM 题目: Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; ...

  7. MIPS简单入门

    What ‘s the MIPS? 汇编语言 汇编语言是一类语言的总称,因处理器不同,而对应的不同机器的指令集也不同,产生了很多种汇编语言. 目前最流行的是ARM,MIPS,x86.ARM用于大量的移 ...

  8. python基础一 day13 复习

    # 函数 —— 2天 # 函数的定义和调用 # def 函数名(形参): #函数体 #return 返回值 #调用 函数名(实参) # 站在形参的角度上 : 位置参数,*args,默认参数(陷阱),* ...

  9. Oracle旗下软件官网下载速度过慢解决办法

    平常下载Oracle旗下软件官网的产品资源,会发现速度很慢,如下载JDK和mysql时, 这样很浪费我们的时间 解决办法: 复制自己需要下载的资源链接 使用迅雷下载该资源 速度均很快 如下载Mysql ...

  10. Bootstrap历练实例:分页的大小

    分页的大小 下面的实例演示了上表中所讨论的 class .pagination-* 的用法: <!DOCTYPE html><html><head><meta ...