Trapping Rain Water II

[leetcode] 407. Trapping Rain Water II 解题报告 题目链接:https://leetcode.com/problems/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 wate 阅读详情

用优先队列,把边沿的墙全加进去,然后从最低的开始,看四周要不要补水。其他的和第一题很像

public class Solution {
    class cell implements Comparable{
        int x;
        int y;
        int height;

        public cell(int x, int y, int height) {
            this.x = x;
            this.y = y;
            this.height = height;
        }
        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }

        public int getHeight() {
            return height;
        }

        public void setHeight(int height) {
            this.height = height;
        }

        @Override
        public int compareTo( Object o) {
            cell oo = (cell) o;
            return height-((cell) o).getHeight();
        }
    }
    public int trapRainWater(int[][] heightMap) {
        int width = heightMap.length;
        if(width<=0)return 0;
        int length = heightMap[0].length;
        boolean [][]visted = new boolean[width][length];
        PriorityQueue<cell> priorityQueue = new PriorityQueue<>();
        if (width == 0 || length == 0)return 0;
        for (int i=0;i<width;i++){
            visted[i][length-1] = true;
            visted[i][0] = true;
            priorityQueue.add(new cell(length-1,i,heightMap[i][length-1]));
            priorityQueue.add(new cell(0,i,heightMap[i][0]));
        }
        for (int i=0;i<length;i++){
            visted[width-1][i] = true;
            visted[0][i] =true;
            priorityQueue.add(new cell(i,width-1,heightMap[width-1][i]));
            priorityQueue.add(new cell(i,0,heightMap[0][i]));
        }
        int res = 0;
        int[][] fangxiang = {{0,1},{0,-1},{-1,0},{1,0}};

        while (!priorityQueue.isEmpty()){
            cell ce = priorityQueue.poll();
            //System.out.println("height:"+ce.getHeight());
            for (int i=0;i<fangxiang.length;i++){
                int dx = ce.getX()+fangxiang[i][0];
                int dy = ce.getY()+fangxiang[i][1];
                if (dx>=0&&dx<length&&dy>=0&&dy<width) {
                    if(!visted[dy][dx]){
                        cell newCell = new cell(dx, dy, heightMap[dy][dx]);
                        visted[dy][dx] = true;
                        if (ce.compareTo(newCell)>0){
                            res+=ce.compareTo(newCell);
                           // System.out.println("res:"+res);
                            newCell.setHeight(ce.getHeight());
                            priorityQueue.add(newCell);
                            continue;

                        }else {
                            priorityQueue.add(newCell);
                        }
                    }
                }
            }

        }
        return res;

    }
}
2025年华为杯中国研究生数学建模竞赛E题 高速列车轴承智能故障诊断问题 思路+代码 all_data, file_info = preprocessor.load_mat_files(file_list[:50]) # 加载前50个文件作为示例。[y_source, y_source, domain_labels], # 注意:这里使用伪标签。X_target = np.random.randn(100, X_source.shape[1], 1) # 示例数据。"""构建CNN-Transformer混合模型""""""构建Transformer模型""" 阅读详情

相关推荐

AI初识—LLM、Ollama、Llama都是些个啥?

LLM指的是大语言模型(Large Language Model),是一种基于深度学习的模型,能够理解和生成自然语言。它通过大量文本数据进行训练,具备了理解上下文、生成连贯文本和回答问题的能力。著名的LLM包括OpenAI的GPT系列和Google的BERT。在AI技术快速发展的背景下,LLM、Ollama和Llama代表了现代自然语言处理的不同方面。LLM为我们提供了强大的语言理解能力,Ollama为模型的部署和管理提供了便利,而Llama则在效率和多样性上不断推进。

小蜗牛的珍贵百宝箱 4629

Leetcode 407. Trapping Rain Water II

Problem: 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 a...

weixin_34233679的博客 98

华为防火墙双ISP出口实战:如何用策略路由实现电信联通流量自动切换(附配置截图)

本文详细解析了华为防火墙在双ISP环境下的智能选路技术,通过策略路由与健康检测的深度应用,实现电信联通流量的自动切换。文章包含实战配置截图,帮助企业解决切换延迟、路径劣化等核心挑战,提升业务连续性。

backprop5master的博客 643

Trapping Rain Water & Container With Most Water & Product of Arrary & Trapping Rain Water II 小结

Trapping Rain Water & Container With Most Water & Product of Arrary 的解题在于使用双指针和保存最值 Trapping Rain Water class Solution { public int trap(int[] height) { int len=height.length;...

anhuibozhoushatu的专栏 203

CLAHE 算法学习 matlab

概念可参考: CLAHE (Contrast Limited Adaptive Histogram Equalization)算法学习CLAHE具体实现主要包括6个步骤。1):分块。将输入图像划分为大小相等的不重叠子块,每个子块含有的像素数为M。参考示例为8×8块。 2):计算子块直方图。 3):计算剪切阈值clipLimit 4):像素点重分配。对每个子块,使用步骤三中多余的像素重新分配。 5):直方图均衡。 6):像素点灰度值重构。(源码中采用了双线性插值) http://www.realtimer

baidu_34971492的博客 4253

qq对接openclaw,通过qq发送命令

把 OpenClaw 接入 QQ 和微信,目前主流的微信文章和教程主要提供了两种截然不同的路径。我为你梳理了它们的核心区别和关键步骤,你可以根据自己的需求来选择。

suny8的专栏 270

407. Trapping Rain Water II

407. Trapping Rain Water II方法1: 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...

wilzxu的博客 162

2026年算法工程师常见面试简答题

本文摘要:文章系统梳理了机器学习与深度学习中的30个核心概念,涵盖模型优化、评估指标、经典算法和大模型应用等关键知识点。主要内容包括:1)过拟合与欠拟合的成因及解决方法;2)偏差-方差权衡原理;3)数据划分与交叉验证策略;4)分类/回归任务评估指标选择;5)逻辑回归、决策树、SVM等传统算法解析;6)梯度下降优化与正则化技术;7)CNN、RNN、Transformer架构对比;8)Embedding表示与RAG技术。特别强调面试高频考点如XGBoost改进、注意力机制原理等,并给出实际工程中的解决方案建议,

不务正业的猿 350

【Leetcode】407. Trapping Rain Water II

题目地址: https://leetcode.com/problems/trapping-rain-water-ii/ 给定一个二维矩阵AAA,每个位置表示这个位置的柱子高度。问下雨的时候这个矩阵上面最多能盛多少水。 思路参考https://blog.youkuaiyun.com/qq_46105170/article/details/108575830。代码如下: import java.util.Objects; import java.util.PriorityQueue; public class Solu

数学、算法爱好者的博客 333

42、多媒体与虚拟现实在艺术和影视领域的创新应用

本文探讨了多媒体与虚拟现实在艺术和影视领域的创新应用,涵盖三个典型案例:影视与人类学融合项目通过协作模式模糊作者与观众界限,推动文化文献的发展;交互式数字短片《分手》利用基于对象的广播技术探索个性化影视体验;威尼斯双年展瑞士馆4D项目则通过虚拟现实技术实现文化遗产的数字化展示。这些案例展示了技术在叙事、用户体验及文化保护中的深度应用,并展望了未来技术融合、个性化深化与文化遗产传承的发展趋势。

kappa的博客 114

组态王与DLT645-2007电能表通讯调试总结

组态王与电力协议DLT 645-2007调试总结 DLT 645-2007是多功能电能表通信协议,该标准由中国电力企业联合会2006年提出,该协议为主-从结构的半双工通信方式。手持单元或其它数据终端为主站,多功能电能表为从站。每个多功能电能表均有各自的地址编码。通信链路的建立与解除均由主站发出的信息帧来控制。帧是传送信息的基本单元,每帧由帧起始符、从站地址域、控制码、数据域长度、数据域、帧信息纵向校验码及帧结束符7个域组成,每部分由若干字节组成,帧格式如图1所示。 1.确定设备地址 地址域由 6 个字节构

逍遥子的博客 1万+

[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 ...

weixin_34406086的博客 151

EMB电子机械制动系统和EHB电子液压制动系统控制功能的对比

EMB电子机械制动系统相比EHB液压制动系统具有显著优势:响应速度更快(<100ms)、控制精度更高(±2N·m)、能实现单轮独立控制,支持0.1%级制动力调节。在ABS、ESC等安全功能上表现更优,制动平顺性和NVH性能更好,同时提升能量回收效率15%-20%。特别适合智能驾驶需求,可使AEB等系统响应时间缩短50ms,紧急制动距离减少1米以上。EMB系统完美契合高阶自动驾驶对快速响应和精准控制的要求。

m0_59012156的博客 885

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 l

夜色之浓,莫过于黎明前的黑暗。 423

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

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.

淡然坊 636

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

LeetCode407. Trapping Rain Water II

Given anm x nmatrix 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: Bothmandnare le...

李歇特冯-兹拜因巴哈的博客 177

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

leetcode 407 Trapping Rain Water II

Given anm x nmatrix 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: Bothmandnare le...

cmy203的博客 198

[LeetCode]407. Trapping Rain Water II

https://leetcode.com/problems/trapping-rain-water-ii/?tab=Description 计算二维数组能储多少水 PriorityQueue(出当前高度最小的点,进的点要保证为更新后的高度) + 二维boolean数组(记录已访问节点) public class Solution { int[][] dirs

gqk289的专栏 276

Lintcode364 Trapping Rain Water II solution 题解

【题目描述】 Givennxmnon-negative integers representing an elevation map 2d where the area of each cell is1x1, compute how much water it is able to trap after raining. ...

aceg86924的博客 174
上一篇: cs186-2
下一篇: Spring MVC 4 + Spring Security 4 + Hibernate +JPA实战
王Clever
博客等级 码龄11年 9粉丝 83原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值