leetcode Trapping Rain Water

[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 阅读详情

做法就是维护一个非严格递减,遇到不递减的高度,就不断地往栈中弹出元素直到栈顶元素的高度比这个大为止或者栈为空,如果栈为空,那么证明这一段的容量就可以计算出来了,如果栈不为空,那么从栈顶位置到当前位置的容量可以用这个两个高度的较低者水的水平线。

我们可以把道题看成有几个容器,我们来确定容器的两个侧边。如果左侧边比右侧边小,那么这一段容量就可以求了,因为如果后面遇到更比右侧边更大的边不会影响这个的结果。暂时先说到这,说的很不清楚,一定要好好想想怎么表达!!赶紧复习密码学去了哭

class Solution {
public:
    int trap(int A[], int n) {
        if(n<=1)return 0;
        vector<int > sum;
        sum.resize(n);
        int ans = 0;
        stack<int>  s;
        for(int i = 0;i<n;i++){
            
            if(s.empty()){
                s.push(i);
                sum[0] = A[0];
            }else{
                sum[i] = sum[i-1]+A[i];
                if(A[s.top()]>=A[i])s.push(i);
                else {
                    int k = s.top();
                    while(A[k]<A[i]){
                        s.pop();
                        if(!s.empty())k = s.top();
                        else break;
                    }
                    if(s.empty()){
                        ans+=A[k]*(i-k-1)-sum[i-1]+sum[k];
                        s.push(i);
                    }else {
                        s.push(i);
                    }
                }
            }
        }
        int t = s.top();
        s.pop();
        while(!s.empty()){
            int u = s.top();
            s.pop();
            ans+=A[t]*(t-u-1)-sum[t-1]+sum[u];
            t= u;
        }
        return ans;
    }
};
LeetCode 接雨水 Trapping Rain Water 积水问题 中文题目使用两根指针:给定 n 个非负整数表示每个宽度为1的柱子的高度图,计算下雨之后能接多少水。给定 n 个非负整数表示每个宽度为1的柱子的高度图,计算下雨之后能接多少水。例如,输入 [0,1,0,2,1,0,1,3,2,1,2,1],返回 6。上面的高度图由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示,在这种情况下,可以接 6 个单位的雨水(蓝色部分)。英文题目Given n... 阅读详情

相关推荐

基于 Python 的 LIF 模型:探索神经元同步与小世界网络

首先,我们定义了一个LIFNeuronself.soma.L = self.soma.diam = 12.6157 # 固定神经元形状self.cm = cm # 电容self.V_rest = V_rest # 静息电位self.V = V_rest # 初始化膜电位在初始化过程中,每个神经元被赋予初始的静息电位,并且其膜电位会在后续的模拟过程中动态变化。

qq_36224726的博客 1429

[LeetCode] Trapping Rain Water 三种方案 Python

Trapping Rain Water: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example,  Given [0,1,

新手村 951

Aurix TC3XX Cache memory分析

Aurix Cache memory 分析

qwe0405080615的博客 4356

LeetCode42——Trapping Rain Water

LeetCode42——Trapping Rain Water 题意: 看图就很明显。 但是做起来就没想象的简单了。。。 我最初的疑惑: 如果序列是这种情况01010,也就是说“凹点”两边都是“凸点”这样就简单了,直接根据短板原理可求,遍历求出索引为i-1和i+1的value的最小值即可。。。 但是如果遇到这种情况21012,也就是中间0的凹点,两边的壁呈递增,就懵逼了。。。

上善若水 1984

Kettle Web版部署指南

看到以上的日志则表示启动成功。

2401_89221867的博客 1720

Nuvoton OpenTitan开源安全芯片技术解析与应用

硬件安全模块(HSM)作为可信计算基的核心组件,其设计透明度和可验证性正成为行业焦点。RISC-V架构凭借开源特性,为构建可审计的安全芯片提供了新范式。OpenTitan项目通过开放Verilog源码和形式化证明,实现了从物理不可克隆函数(PUF)到分层安全启动的全栈透明。该方案在lowRISC Voyager开发板上实测显示,安全启动时间小于200ms,性能损耗仅7%,显著优于传统黑盒方案。目前该技术已应用于Chromebook和数据中心,其40nm工艺版本更将功耗降至5μA待机水平,为物联网和边缘计算提

weixin_36303807的博客 406

【LeetCode】42. Trapping Rain Water【双指针&栈】

n个非负整数,数值代表高度,问这样的形状能收集多少雨水。原题的图片有助于理解题意。 Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The...

zhaoqinmuxue的博客 713

编译原理(东南大学)一:【编译原理期末冲刺救命笔记】速存!看完再上考场稳了!

本文详细介绍了编译原理的核心概念和流程,包括程序设计语言的分类、编译程序的各个阶段(如词法分析、语法分析、语义分析、中间代码生成、优化和目标代码生成)以及编译程序生成和构造的基础知识。文章还探讨了编译中的表格管理、出错处理和遍的概念,以及编译基础知识如高级语言、语法规则、字母表和符号串的运算。此外,文中还涉及文法与语言的关系,包括文法的概念、非终结符、终结符、产生式、推导和规约等。整体内容旨在为学习编译原理的学生提供全面的复习资料和考试准备指南。

一位搞嵌入式和前端的genius的博客 1029

LeetCode—Trapping Rain Water 储水问题,从两边向中间

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example,  Given [0,1,0,2,1,0,1,3,2,1,2,1]

xietingcandice的专栏 1011

ABAP Cloud define table 里的 not null 究竟在约束什么?——语义、场景与可运行示例

ABAP Cloud 中 not null 与 NULL 的用法解析 本文探讨了 ABAP Cloud 中 not null 关键字的语义与使用场景,对比了传统 ABAP 与 ABAP Cloud 在表字段定义上的差异: 核心概念 not null 是数据库层的列约束,强制字段不能存储 NULL 值 ABAP Cloud 通过 DDL 显式声明列是否允许 NULL,HANA 上 CDS 表默认创建为 NOT NULL 使用建议 主键、外键、审计字段等必须使用 not null 业务上可缺失的字段(如批准日期

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 304

【C++/嵌入式梳理】bootloader

【C++/嵌入式笔试面试八股】bootloader

Hi 1212

Trapping Rain Water [LeetCode]

Problem Description:http://oj.leetcode.com/problems/trapping-rain-water/ Basic idea: Get the index of max number of the array, then split the array at that point. Find the left max num, calcaut...

aa66662352的博客 122

EMB电子机械制动执行器五大核心部件选型解析

本文介绍了EMB电子机械制动执行器五大核心部件的选型方案:采用无刷直流电机作为驱动源,兼具高扭矩和快速响应特性;选用滚珠丝杠传动机构,实现高效旋转-直线转换;行星齿轮减速器满足紧凑空间内的增扭需求;QT450-QT550材质的钳体确保刚度要求;应变片式压力传感器实现夹紧力精确测量。各部件选型综合考虑了性能、可靠性和空间限制,为EMB系统设计提供了完整解决方案。

m0_59012156的博客 1759

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

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

LeetCode: Trapping Rain Water 解题报告

https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is abl...

weixin_30408675的博客 204

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

LeetCode 42. Trapping Rain Water(Hard)

查看原文,请点击彼得.攀的小站 查阅更多的题解,请点击 Problem 42. Trapping Rain Water(Hard) Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to tra...

彼得.攀的blog 382

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

Leetcode 每日一题:Trapping Rain Water

Google 高频真题:Leetcode 42: Trapping Rain Water 题目分析与详解

2401_85421495的博客 1148

LeetCode-42-Trapping Rain Water 贪心或单调栈

这个题一开始想麻烦了,我用单调栈实现了一下,A了,但是只打败了0.4%的人,后来看了一眼别人的博客,发现没那么麻烦,找到最高点,从左右贪心就行了。。。 下面是两份不同做法的代码,时间复杂度都是O(n) class Solution(object): def trap(self, height): """ :type height: List[in

世靖的码场 927

LeetCode 42. Trapping Rain Water--算法题--c++解法

LeetCode 42. Trapping Rain Water–c++解法 LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在这个专栏里,大部分题目Java和Python的解法都有。 Given n non-negative integers representing an elevation map where the width of each ba...

个人博客 928

leetcode记录42. Trapping Rain Water

leetcode记录2. Trapping Rain Water Category Difficulty Likes Dislikes algorithms Hard (44.24%) 4173 77 √ Accepted √ 315/315 cases passed (4 ms) √ Your runtime beats 95.99 % of cpp submissi...

我的博客 191

[LeetCode] Trapping Rain Water

http://oj.leetcode.com/problems/trapping-rain-water/ 思路 就是 先从左往右走, 将递增产生的水坑给算出来, 再从右往左左走, 将递增的水坑给算出来. 代码写的很劣, 居然ac了, 我得看看题解去. 1 class Solution { 2 public: 3 int trap(int A[], int n) { ...

weixin_30287169的博客 92

Leetcode1-100: 42. Trapping Rain Water

Leetcode1-100: 42. Trapping Rain Water问题描述解题思路1. 统计横向的格子数量2. 统计每一列的储水量3. 利用动态规划来改进思路2代码实现implement 1implement 2implement 3分析后记 问题描述 Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is a

Y123iwantit的博客 300

Leetcode 042 Trapping Rain Water(高效)

题目连接:Leetcode 042 Trapping Rain Water解题思路:从左向右遍历一遍,保存每个位置往左的最高值。再从右往左遍历一遍,保存每个位置往右的最高值。最后遍历一遍数组,取左右最高值中较小的一个,减去当前值,即为这个位置增加的量。class Solution { public: int trap(vector&lt;int&gt;&amp; height) { i...

不慌不忙、不急不躁 277

LeetCode42. Trapping Rain Water

LeetCode42. Trapping Rain Water

bookman的博客 507

【leetcode】【hard】【每日一题】42. Trapping Rain Water​​​​​​​

42. Trapping Rain Water Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation ma...

lemonade13的博客 133

LeetCode 42. Trapping Rain Water

第一次写博客,我这种水平的大学生程序猿为什么会想到要写博客这种东西呢,而且还要周更呢?没错,这是我们老师布置的作业。如果觉得我的博客写得有何不妥,请在下方留言,每周我会抽取幸运观众进行回复,谢谢大家。 进入正题,这是我们算法老师布置的作业,每周我将在LeetCode上抽取幸运题进行解答,并编写题解在博客上。打开LeetCode,因为我之前尝试过一道难度为中等的题,觉得还行,所以这一次我直接找了一

Ares-T的博客 704
上一篇: leetcode word break
下一篇: leetcode Longest Valid Parentheses O(n)解法
贱走偏锋
博客等级 码龄15年 24粉丝 60原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值