407. 接雨水 II

这是一个关于LeetCode第407题的解决方案,题目要求计算一个给定地形中能容纳的雨水量。算法通过优先队列找到当前区域的最低点,并结算周围可以填充的雨水,逐步遍历整个地形。代码使用了Java实现,主要涉及数据结构和算法的知识,包括优先队列、二维数组操作和广度优先搜索策略。
摘要由CSDN通过智能技术生成

题目:

https://leetcode-cn.com/problems/trapping-rain-water-ii/

import java.util.Comparator;
import java.util.PriorityQueue;

public class _407_TrapRainWater {
    public  class Location {
        int row, col, height;

        public Location(int row, int col, int height) {
            this.row = row;
            this.col = col;
            this.height = height;
        }
    }

    public  int trapRainWater(int[][] heightMap) {
        int rows = heightMap.length;//宽
        int cols = heightMap[0].length;//长
        boolean[][] isResolve = new boolean[rows][cols];
        //构造外围圈,初始化
        PriorityQueue<Location> locations = new PriorityQueue<>(Comparator.comparingInt(x -> x.height));//堆里存的
        for (int col = 0; col < cols; col++) {
            locations.add(new Location(0, col, heightMap[0][col]));
            isResolve[0][col] = true;
        }
        //第一列
        for (int row = 1; row < rows - 1; row++) {
            locations.add(new Location(row, 0, heightMap[row][0]));
            isResolve[row][0] = true;
        }
        //最后一列
        for (int row = 1; row < rows - 1; row++) {
            locations.add(new Location(row, cols - 1, heightMap[row][cols - 1]));
            isResolve[row][cols - 1] = true;
        }
        //最后一行
        for (int col = 0; col < cols; col++) {
            locations.add(new Location(rows - 1, col, heightMap[rows - 1][col]));
            isResolve[rows - 1][col] = true;
        }
        //OK,开始找短板
        int shortSlap = 0;
        int waterAll = 0;
        while (!locations.isEmpty()) {
            Location curCircleShortSlap = locations.poll();
            shortSlap = Math.max(shortSlap, curCircleShortSlap.height);//决出短板
            //结算短板四周的水量
            //左
            int row = curCircleShortSlap.row;
            int col = curCircleShortSlap.col;
            if ((col - 1 >= 0) && (!isResolve[row][col - 1])) {
                waterAll += Math.max(0, shortSlap - heightMap[row][col - 1]);//结算水量,用短板减去这里的地势,差值就是hold住的水
                isResolve[row][col - 1] = true;
                locations.add(new Location(row, col - 1, heightMap[row][col - 1]));//你该做边界了
            }
            //上
            if ((row - 1 >= 0) && (!isResolve[row - 1][col])) {
                waterAll += Math.max(0, shortSlap - heightMap[row - 1][col]);
                isResolve[row - 1][col] = true;
                locations.add(new Location(row - 1, col, heightMap[row - 1][col]));
            }
            //右
            if ((col + 1 < cols) && (!isResolve[row][col + 1])) {
                waterAll += Math.max(0, shortSlap - heightMap[row][col + 1]);
                isResolve[row][col + 1] = true;
                locations.add(new Location(row, col + 1, heightMap[row][col + 1]));
            }
            //下
            if ((row + 1 < rows) && (!isResolve[row + 1][col])) {
                waterAll += Math.max(0, shortSlap - heightMap[row + 1][col]);
                isResolve[row + 1][col] = true;
                locations.add(new Location(row + 1, col, heightMap[row + 1][col]));
            }
        }

        return waterAll;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值