Leetcode-407. Trapping Rain Water II [C++][Java]

目录

一、题目描述

二、解题思路

【C++】

【Java】


Leetcode-407. Trapping Rain Water IIhttps://leetcode.com/problems/trapping-rain-water-ii/description/

一、题目描述

Given an m x n integer matrix heightMap representing the height of each unit cell in a 2D elevation map, return the volume of water it can trap after raining.

Example 1:

Input: heightMap = [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]]
Output: 4
Explanation: After the rain, water is trapped between the blocks.
We have two small ponds 1 and 3 units trapped.
The total volume of water trapped is 4.

Example 2:

Input: heightMap = [[3,3,3,3,3],[3,2,2,2,3],[3,2,1,2,3],[3,2,2,2,3],[3,3,3,3,3]]
Output: 10

Constraints:

  • m == heightMap.length
  • n == heightMap[i].length
  • 1 <= m, n <= 200
  • 0 <= heightMap[i][j] <= 2 * 104

二、解题思路

最小堆+BFS

  • 时间复杂度O(mnlog(mn))

  • 空间复杂度O(mn)

【C++】

class Solution {
private:
    int dirs[5] = {-1, 0, 1, 0, -1};
public:
    int trapRainWater(vector<vector<int>>& heightMap) {
        if (heightMap.size() < 3 || heightMap[0].size() < 3) {
            return 0;
        }
        int m = heightMap.size(), n = heightMap[0].size(), res = 0;
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
        vector<vector<bool>> visit(m, vector<bool>(n, false));
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (i == 0 || i == m - 1 || j == 0 || j == n - 1) {
                    pq.push({heightMap[i][j], i * n + j});
                    visit[i][j] = true;
                }
            }
        }
        while (!pq.empty()) {
            pair<int, int> cur = pq.top(); pq.pop();
            for (int k = 0; k < 4; ++k) {
                int nx = cur.second / n + dirs[k], ny = cur.second % n + dirs[k + 1];
                if (nx >= 0 && nx < m && ny >= 0 && ny < n && !visit[nx][ny]) {
                    if (heightMap[nx][ny] < cur.first) {
                        res += cur.first - heightMap[nx][ny];
                    }
                    visit[nx][ny] = true;
                    pq.push({max(heightMap[nx][ny], cur.first), nx * n + ny});
                }
            }
        }
        return res;
    }
};

【Java】

class Solution {
    private int[] dirs = {-1, 0, 1, 0, -1};
    public int trapRainWater(int[][] heightMap) {
        if (heightMap == null || heightMap.length < 3 || heightMap[0].length < 3) {
            return 0;
        }
        int m = heightMap.length, n = heightMap[0].length, res = 0;
        PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]);
        boolean[][] visit = new boolean[m][n];
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (i == 0 || i == m - 1 || j == 0 || j == n - 1) {
                    pq.offer(new int[]{heightMap[i][j], i * n + j});
                    visit[i][j] = true;
                }
            }
        }
        while (!pq.isEmpty()) {
            int[] cur = pq.poll();
            for (int k = 0; k < 4; ++k) {
                int nx = cur[1] / n + dirs[k], ny = cur[1] % n + dirs[k + 1];
                if (nx >= 0 && nx < m && ny >= 0 && ny < n && !visit[nx][ny]) {
                    if (heightMap[nx][ny] < cur[0]) {
                        res += cur[0] - heightMap[nx][ny];
                    }
                    visit[nx][ny] = true;
                    pq.offer(new int[]{Math.max(heightMap[nx][ny], cur[0]), nx * n + ny});
                }
            }
        }
        return res;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贫道绝缘子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值