接雨水

给定一个 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. asp.net excel导出红色字体

    文章转自网上的一位朋友,非常感谢! 后台代码 public void ExportDataTableToExcel(System.Data.DataTable s_DataTable) { int t ...

  2. cf559C. Gerald and Giant Chess(容斥原理)

    题意 $h \times w$的网格,有$n$个障碍点, 每次可以向右或向下移动 求从$(1, 1)$到$(h, w)$不经过障碍点的方案数 Sol 容斥原理 从$(1, 1)$到$(h, w)$不经 ...

  3. Ubuntu启动项

    原文地址:http://blog.163.com/yangshuai126%40126/blog/static/1734262652010928101641555/ Ubuntu开机之后会执行/etc ...

  4. 判断后台service是否在运行

    public static boolean isServiceRunning(Context mContext,String className) { boolean isRunning = fals ...

  5. idea npm 调试报错解决办法

    1.用egg框架的开发时候,egg 提供本地开发和调试.点击idea 的debug 按钮时候报如下错误: Please specify npm or yarn package: cannot find ...

  6. 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} 解决方法

    Tomcat启动时出现红色警告内容 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'sour ...

  7. IOS之TextView属性设置

    UIFontDescriptor *bodyFontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFont ...

  8. 使用 Azure 创建存储和检索文件

    本指南将以循序渐进的方式帮助您使用 Azure 将文件存储到云中.我们将逐一介绍如何创建存储账户.创建容器.上传文件.检索文件和删除文件.在本教程中完成的所有操作均符合 1 元试用条件. 本指南将以循 ...

  9. Django之CSRF问题

    1.csrf全称:cross site request forgery(跨站请求伪造),举例来讲,一个安全的网站A,一个恶意网站B,当你在A网站进行了登录后,这时候浏览器会保存你的cookie和ses ...

  10. 6.3 lambda 表达式

    6.3.1 lambda 表达式是一个可传递的代码块,可以在以后执行一次或者多次. 思考(如何按指定时间间隔完成工作,将这个工作放在一个ActionListener的actionPerformed方法 ...