前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetcode407. Trapping Rain Water II

leetcode407. Trapping Rain Water II

作者头像
眯眯眼的猫头鹰
发布2019-10-08 18:51:43
4970
发布2019-10-08 18:51:43
举报

题目要求

代码语言:javascript
复制
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.
clipboard.png
clipboard.png
代码语言:javascript
复制
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.
clipboard.png
clipboard.png
代码语言:javascript
复制
After the rain, water is trapped between the blocks. The total volume of water trapped is 4.

神仙题。能够想出来用优先队列和广度优先遍历结合的都是大佬。希望所有看到这道题目的可以在文章回复里面分享一下写这题的思路。在下面我就粘贴一下根据油管上的思路写成的JAVA解答。

思路和代码

思路的动画

代码语言:javascript
复制
public int trapRainWater(int[][] heightMap) {
        if (heightMap == null || heightMap.length <= 2 || heightMap[0].length <= 2) {
            return 0;
        }
        int rowCount = heightMap.length;
        int columnCount = heightMap[0].length;
        boolean[][] visited = new boolean[rowCount][columnCount];
        PriorityQueue<Position> queue = new PriorityQueue<>();
        for (int i = 0 ; i < rowCount ; i++) {
            visited[i][0] = true;
            queue.offer(new Position(i, 0, heightMap[i][0]));
            visited[i][columnCount-1] = true;
            queue.offer(new Position(i, columnCount-1, heightMap[i][columnCount-1]));
        }

        for (int i = 1 ; i < columnCount ; i++) {
            visited[0][i] = true;
            queue.offer(new Position(0, i, heightMap[0][i]));
            visited[rowCount-1][i] = true;
            queue.offer(new Position(rowCount-1, i, heightMap[rowCount-1][i]));
        }

        int water = 0;
        int max = 0;
        while (!queue.isEmpty()) {
            Position position = queue.poll();
            if (position.value > max) {
                max = position.value;
            }else {
                water += max - position.value;
            }
            int rowIndex = position.rowIndex;
            int columnIndex = position.columnIndex;
            if (rowIndex > 0 && !visited[rowIndex-1][columnIndex]) {
                queue.offer(new Position(rowIndex-1, columnIndex, heightMap[rowIndex-1][columnIndex]));
                visited[rowIndex-1][columnIndex] = true;
            }
            if (rowIndex < rowCount-1 && !visited[rowIndex+1][columnIndex]) {
                queue.offer(new Position(rowIndex+1, columnIndex, heightMap[rowIndex+1][columnIndex]));
                visited[rowIndex+1][columnIndex] = true;
            }
            if (columnIndex > 0 && !visited[rowIndex][columnIndex-1]) {
                queue.offer(new Position(rowIndex, columnIndex-1, heightMap[rowIndex][columnIndex-1]));
                visited[rowIndex][columnIndex-1] = true;
            }
            if (columnIndex < columnCount - 1 && !visited[rowIndex][columnIndex+1]) {
                queue.offer(new Position(rowIndex, columnIndex+1, heightMap[rowIndex][columnIndex+1]));
                visited[rowIndex][columnIndex+1] = true;
            }
        }
        return water;
    }

    public static class Position implements Comparable<Position> {
        int rowIndex;
        int columnIndex;
        int value;

        Position(int rowIndex, int columnIndex, int value) {
            this.rowIndex = rowIndex;
            this.columnIndex = columnIndex;
            this.value = value;
        }

        @Override
        public int compareTo(Position o) {
            return this.value - o.value;
        }

    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目要求
  • 思路和代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档