首页 > 其他分享 >42. 接雨水

42. 接雨水

时间:2023-10-27 18:44:21浏览次数:29  
标签:val idx 42 雨水 height single border stack

链接

https://leetcode.cn/problems/trapping-rain-water/description/

思路

1. 在线处理。既然是接雨水,那肯定是形成一个类似于碗的结构才能接。可以先找到一个最大值当兜底,然后不断的用当前border去夹逼。如果遇到比当前border高的,那应该更新border。

2. 单调栈。跟在线处理思路类似,但又不同。单调栈解法可以想象成雨水是从天而降,一层一层的往上铺。

代码一

class Solution:
    def trap(self, height: List[int]) -> int:
        max_height = max(height)
        max_idx = height.index(max_height)
        min_height = height[0]
        res = 0
        for i in range(max_idx):
            if height[i] <= min_height:
                res += min_height-height[i]
            else:
                min_height = height[i]
        min_height = height[-1]
        for i in range(len(height)-1, max_idx, -1):
            if height[i] <= min_height:
                res += min_height-height[i]
            else:
                min_height = height[i]
        return res

 

代码二

class Solution(object):
    def trap(self, height):
        single_stack = []
        res = 0
        for i in range(len(height)):
            right_border_idx = i
            while single_stack and height[single_stack[-1]] < height[i]:
                bottom_idx = single_stack.pop()
                bottom_val = height[bottom_idx]
                while single_stack and bottom_val == height[single_stack[-1]]:
                    single_stack.pop()
                if single_stack:
                    left_border_idx = single_stack[-1]
                    left_border_val = height[left_border_idx]
                    res += (min(left_border_val, height[i])-bottom_val) * (right_border_idx-left_border_idx-1)
            single_stack.append(right_border_idx)
        return res

 

标签:val,idx,42,雨水,height,single,border,stack
From: https://www.cnblogs.com/bjfu-vth/p/17792975.html

相关文章

  • Python42days
    外键(表与表之间的关系)一对多 一对一多对多多表查询相关**’Navicat可视化软件—————————————————————————————————————————————————————————————————————————————————————————......
  • 【洛谷 8742】[蓝桥杯 2021 省 AB] 砝码称重
    题目描述你有一架天平和 �N 个砝码,这 �N 个砝码重量依次是 �1,�2,⋯ ,��W1​,W2​,⋯,WN​ 。请你计算一共可以称出多少种不同的重量?注意砝码可以放在天平两边。输入格式输入的第一行包含一个整数 �N 。第二行包含 �N 个整数: �1,�2,�3,⋯ ,��W1​,W2​,W3​,⋯,WN​......
  • log4j漏洞CVE-2021-44228复现-排雷篇
    一、环境搭建(用相同的环境才能保证一定成功)下载vulhub,其他环境可能存在GET请求无效问题:gitclonehttps://github.com/vulhub/vulhub.git切换到指定漏洞环境目录:vulhub/log4j/CVE-2021-44228开启docker:dockercomposeup-d➜vulhubgit:(master)cdvulhub/log4j/CVE-20......
  • P4253 [SCOI2015] 小凸玩密室
    P4253bzoj#4446非常好的一道树形dp题起初我看错题了QwQ,以为第一个选的必须为根首先我们发现假设我们选的第一个灯泡为\(u\),他的行走过程是:\(u\rightarrowu\)子树\(\rightarrowfa_u\rightarrowu\)兄弟子树\(\rightarrowfa_{fa_u}\rightarrow\dots\)因此我......
  • OGG-03542 Failed to connect to the database
    GGSCI(dwdb01)4>dbloginuseridGOLDENGATE@cnlionrdb,password*******2023-10-2317:11:26INFOOGG-03542Failedtoconnecttothedatabase.Verifythattheconnectionstringandfollowingenvironmentvariablesarecorrect:LD_LIBRARY_PATH=/ho......
  • Oracle10gOCP042题库121166题共168题
    121.Youwanttocreateanewoptimizeddatabaseforyourtransactionalproductionenvironmenttobeusedbyafinancialapplication.Whilecreatingthedatabase,youwanttheOraclesoftwaretotakecareofallbasicsettingstooptimizethedatabasep......
  • Oracle10gOCP042题库130题共168题
    声明:对于答案的相关的说明,是个人对Oracle的理解。1.Becauseofapoweroutage,instancefailurehasoccurred.Fromwhatpointintheredologdoesrecoverybeginandwheredoesitend?A.CurrentredologandinactiveredologB.Checkpointpositiontoendofr......
  • Oracle10gOCP042题库3170题共168题
    31.WhichtwostatementsaretrueregardingthedatabaseinARCHIVELOGmode?(Choosetwo.) A)Youhavetoshutdownthedatabasetoperformthebackups. B)Archivinginformationiswrittentothedatafilesandredologfiles. C)Youcanperformcomp......
  • Oracle10gOCP042题库71120题共168题
    71.Yourdatabaseinstanceisstartedusingtheserverparameterfile(SPFILE).Controlfilesaremultiplexedandstoredondifferentdisks.Becauseofadiskfailure,youlostoneofthesecontrolfiles.Youreplacedthedamageddisk.Whatisthecorre......
  • 242. 有效的字母异位词
    目录题目法一、字典题目给定两个字符串s和t,编写一个函数来判断t是否是s的字母异位词。注意:若 s和t 中每个字符出现的次数都相同,则称 s和t 互为字母异位词。示例1:输入:s="anagram",t="nagaram"输出:true示例2:输入:s="rat",t="car"......