zoukankan      html  css  js  c++  java
  • Leetcode 407.接雨水

    接雨水

    给定一个 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的,要么保持原样,要么变成装了水之后的高度。

     1 import java.util.Comparator;
     2 import java.util.PriorityQueue;
     3 
     4 public class Solution {
     5 
     6     private static class Cell {
     7         private int row;
     8         private int col;
     9         private int height;
    10 
    11         public Cell(int row, int col, int height){
    12             this.row = row;
    13             this.col = col;
    14             this.height = height;
    15         }
    16     }
    17 
    18 
    19     public static int trapRainWater(int[][] heightMap) {
    20         if(heightMap == null || heightMap.length == 0 ||heightMap[0].length == 0) {
    21             return 0;
    22         }
    23 
    24         PriorityQueue<Cell> queue = new PriorityQueue<>(1, new Comparator<Cell>(){
    25             public int compare(Cell a, Cell b) {
    26                 return a.height - b.height;
    27             }
    28         });
    29 
    30         int m = heightMap.length;
    31         int n = heightMap[0].length;
    32         boolean[][] visited = new boolean[m][n];
    33 
    34         for (int i = 0; i< m ;i++){
    35             visited[i][0] = true;
    36             visited[i][n-1] = true;
    37             queue.offer(new Cell(i, 0, heightMap[i][0]));
    38             queue.offer(new Cell(i, n-1, heightMap[i][n-1]));
    39         }
    40 
    41         for (int i = 0; i< n ;i++){
    42             visited[0][i] = true;
    43             visited[m-1][i] = true;
    44             queue.offer(new Cell(0, i, heightMap[0][i]));
    45             queue.offer(new Cell(m-1, i, heightMap[m-1][i]));
    46         }
    47 
    48 
    49 
    50         int[][] dirs = new int[][]{{-1,0},{1,0},{0,-1},{0,1}};
    51         int res = 0;
    52         while(!queue.isEmpty()){
    53             Cell cell = queue.poll();
    54             for(int[] dir : dirs){
    55                 int row = cell.row + dir[0];
    56                 int col = cell.col + dir[1];
    57                 if(row >= 0 && row < m && col >=0 && col < n && !visited[row][col]){
    58                     visited[row][col] = true;
    59                     res += Math.max(0,cell.height - heightMap[row][col]);
    60                     queue.offer(new Cell(row,col,Math.max(heightMap[row][col],cell.height)));
    61                 }
    62             }
    63         }
    64         return res;
    65     }
    66 
    67     public static void main(String[] args){
    68         int[][] heightMap={{1,4,3,1,3,2},{3,2,1,3,2,4},{2,3,3,2,3,1}};
    69         trapRainWater(heightMap);
    70     }
    71 }
    																																																																																																																																																																																																	int[][] heightMap={{1,4,3,1,3,2},{3,2,1,3,2,4},{2,3,3,2,3,1}};
    																																																																																																																																																																																																																																															trapRainWater(heightMap);    }}
    
  • 相关阅读:
    UVA
    UVA
    UVA
    UVA
    UVA
    UVA
    UVA
    UVA
    UVA
    使用Jmeter(三十)针对ActiveMQ JMS POINT TO POINT压力测试(转载)
  • 原文地址:https://www.cnblogs.com/kexinxin/p/10241839.html
Copyright © 2011-2022 走看看