407. Trapping Rain Water II

Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining.

Note:
Both m and n are less than 110. The height of each unit cell is greater than 0 and is less than 20,000.

Example:

Given the following 3x6 height map:
[
  [1,4,3,1,3,2],
  [3,2,1,3,2,4],
  [2,3,3,2,3,1]
]

Return 4.

The above image represents the elevation map [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]] before the rain.

After the rain, water are trapped between the blocks. The total volume of water trapped is 4.

Analysis, 根据木桶原理,先找外围最矮的bar,里边如果有bar比它还矮,一定能存水(因为四周所有的bar都比它高)

注意还可能存更多的水,因为往里面,很可能cell高度变化。所以要把BFS中间遇到的高的bar都存进queue,随着水平面提升,提升到这些bar的高度,看能不能有凹槽存更多的水

44-45行逻辑就是

if (height[row][col] < cur) {

  res += cur.height- height[row][col];

  queue.offer(new Cell(row, col, cur.height));

}

else {

  queue.offer(new Cell(row, col, height[row][col]));

}

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;
    }
}

  

时间: 2024-08-07 18:32:46

407. Trapping Rain Water II的相关文章

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 elevation map, compute the volume of water it is able to trap after raining. Note:Both m and n are less than 110. The height of each unit cell is greater tha

[LintCode] Trapping Rain Water II

Trapping Rain Water II Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1 x 1, compute how much water it is able to trap after raining. Example Given 5*4 matrix [12,13,0,12] [13,4,13,12] [13,8,10,12] [

[poj] The Wedding Juicer | [lintcode] Trapping Rain Water II

问题描述 给定一个二维矩阵,每个元素都有一个正整数值,表示高度.这样构成了一个二维的.有高度的物体.请问该矩阵可以盛放多少水? 相关题目:POJ The Wedding Juicer Description Farmer John's cows have taken a side job designing interesting punch-bowl designs. The designs are created as follows: * A flat board of size W cm

[LeetCode] Trapping Rain Water II 收集雨水之二

Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining. Note: Both m and n are less than 110. The height of each unit cell is greater th

Leetcode: Trapping Rain Water II

Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining. Note: Both m and n are less than 110. The height of each unit cell is greater th

(算法)Trapping Rain Water II

题目: Given n * m non-negative integers representing an elevation map 2d where the area of each cell is 1 * 1, compute how much water it is able to trap after raining. 思路: 这道题是前面一道题的一个延伸,http://www.cnblogs.com/AndyJee/p/4821590.html 前面的给出是一维数组,而这里提供的是二

【Lintcode】364.Trapping Rain Water II

题目: Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1 x 1, compute how much water it is able to trap after raining. Example Given 5*4 matrix [12,13,0,12] [13,4,13,12] [13,8,10,12] [12,13,12,12] [13,13

Lintcode364 Trapping Rain Water II solution 题解

[题目描述] Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1x1, compute how much water it is able to trap after raining. 给定n x m非负整数,表示二维的海拔地图,每个单元的面积是1 x 1,计算下雨后能捕到多少水. [题目链接] www.lintcode.com/en/problem

11 Container With Most Water 42.Trapping Rain Water

11 和 42 本质上都是木桶原理: 11 如何才能存最多的水? 假设 a[left] < a[right] ,    total = a[left] *(right-left) ,  那么 right -1, right-2 位置 都比 total 小, 此时就没必要move right 了, 因为所有的right -x 都比 right 小. 此时只需要move left 指针, 继续找最大value. code 很简单: public int maxArea(int[] height) {