接雨水

给定一个 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. JDk安装及环境变量的配置

    一.JDK的安装 1.打开下载好的安装包(我在这里附上一个百度云连接,https://pan.baidu.com/s/1o3nx0kbmecAISeneGqykLQ    提取码:jnw6) 傻瓜式安 ...

  2. I/O————字符流和流的关闭

    Reader字符输入流 Writer字符输出流 用于字符的形式读取和写入数据 FileReader与FileWriter分别是Reader与Writer的子类 public class CharScr ...

  3. I/O————字节流

    InputStream字节输入流 OutputStream字节输出流 用于以字节的形式读取和写入数据 下面是使用 字节输入流读取文件字节输出流写入文件 文件可能不存在,所以使用try catch pu ...

  4. {ubuntu}乱七八糟重命名为1 2 3.....png

    i=; for f in *.png; do mv "$f" ${i}.png; ((i++)); done 网上找了个测试的数据集,发现读取时候,要重命名一下 一片帖子问了类似的 ...

  5. PHP高端课程

    关于目后佐道IT教育 http://www.cnblogs.com/itpua/p/7710917.html 目后佐道IT教育的师资团队 http://www.cnblogs.com/itpua/p/ ...

  6. HDOJ4550 卡片游戏 随便销毁内存的代价就是wa//string类的一些用法

    思路 标记最小的最后的位置  放在第一位 标记位置之前按left值小的左方大的右方 标记位置之后按顺序放在最后 不多说先贴上销毁内存的wa代码 销毁内存的wa代码 #include<cstdio ...

  7. (七)VMware Harbor 问题:Get https://192.168.3.135:8088/v2/: http:server gave HTTP response to HTTPS client

    (一)问题描述 登陆时,报错 docker Get https://192.168.3.135:8088/v2/: http:server gave HTTP response to HTTPS cl ...

  8. elastic-job lite 编程实战经验

    (继续贴一篇之前写的经验案例) elastic-job lite 编程实战经验 其实这是一次失败的项目,虽然最后还是做出来了,但是付出了很大代价.并且需要较深入的踩坑改造elastic-job,导致代 ...

  9. urllib基础-构造请求对象,设置用户代理User-Agent

    有的网页具有一些反爬机制,如:需要浏览器请求头中的User-Agent.User-Agent类似浏览器的身份证. 程序中不设置User-Agent.默认是Python-urllib/3.5.这样网站就 ...

  10. tomcat BIO 、NIO 、AIO

    11.11活动当天,服务器负载过大,导致部分页面出现了不可访问的状态.那后来主管就要求调优了,下面是tomcat bio.nio.apr模式以及后来自己测试的一些性能结果. 原理方面的资料都是从网上找 ...