Leetcode 42 407 Trapping Rain Water I II 双指针

本文介绍了一种方法,用于计算在给定的等高线地图上,雨后可以积聚的水量。此外,还探讨了如何计算二维矩阵中雨水的累积体积,通过遍历矩阵并应用特定规则来估算每个单元格的积水情况。

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], return 6.

The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!

就是四面都是围墙,从最低的往里走;
如果里面有更低的,当然就可以蓄水,蓄水的量就是围墙最低 减去 此处的高度;
如果里面的比当前围墙高,那这个方向的围墙高度就增加了。
然后永远围墙最低的地方开始搜,最后就能把整个水池搜一遍

class Solution:
    def trap(self, height) -> int:
        l,r,wall,lower,res = 0,len(height)-1,0,0,0
        while (l<r):
            lower = min(height[l],height[r])
            res += max(0, wall-lower)
            (l,r) = (l+1,r) if height[l] <= height[r] else (l,r-1)
            wall = max(wall,lower)
        return res

s = Solution()
print(s.trap([0,1,0,2,1,0,1,3,2,1,2,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.

Example:

Given the following 3x6 height map:
[
  [1,4,3,1,3,2],
  [3,2,1,3,2,4],
  [2,3,3,2,3,1]
]

Return 4.

The above image represents the elevation map [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]] before the rain.

 

After the rain, water is trapped between the blocks. The total volume of water trapped is 4.

 

Constraints:

  • 1 <= m, n <= 110
  • 0 <= heightMap[i][j] <= 20000

------------------------------------------

import heapq
class Solution:
    def trapRainWater(self, heightMap) -> int:
        rows,cols = len(heightMap),len(heightMap[0]) if heightMap else 0
        if (rows == 0 or cols == 0):
            return 0
        pq,res,height = [],0,0
        for i in range(rows):
            for j in range(cols):
                if (i in {0,rows-1} or j in {0,cols-1}):
                    pq.append((heightMap[i][j],i,j))
                    heightMap[i][j] = -1
        heapq.heapify(pq)
        while (pq):
            ch,x,y = heapq.heappop(pq)
            height = max(ch, height)  # bug2: height = max(nh, height)
            for dx,dy in [(0,1),(1,0),(0,-1),(-1,0)]:
                nx,ny = x+dx,y+dy
                if (0<=nx<rows and 0<=ny<cols and heightMap[nx][ny] >= 0): #bug4: heightMap[nx][ny] > 0
                    nh = heightMap[nx][ny]
                    res += max(0, (height-nh))
                    heapq.heappush(pq,(nh,nx,ny))
                    heightMap[nx][ny] = -1 #bug1
                    #break #bug3: 周围是要保证都走一遍的
        return res

 

Nano-ESG数据资源库的构建基于2023年初至2024年秋季期间采集的逾84万条新闻文本,从中系统提炼出企业环境、社会及治理维度的信息。其构建流程首先依据特定术语在德语与英语新闻平台上检索,初步锁定与德国DAX 40成分股企业相关联的报道。随后借助嵌入技术对文本段落执行去重操作,以降低内容冗余。继而采用GLiNER这一跨语言零样本实体识别系统,排除与目标企业无关的文档。在此基础上,通过GPT-3.5与GPT-4o等大规模语言模型对文本进行双重筛选:一方面判定其与ESG议题的相关性,另一方面生成简明的内容概要。最终环节由GPT-4o模型完成,它对每篇文献进行ESG情感倾向(正面、中性或负面)的判定,并标注所涉及的ESG具体维度,从而形成具备时序特征的ESG情感与维度标注数据集。 该数据集适用于多类企业可持续性研究,例如ESG情感趋势分析、ESG维度细分类别研究,以及企业可持续性事件的时序演变追踪。研究者可利用数据集内提供的新闻摘要、情感标签与维度分类,深入考察企业在不同时期的环境、社会及治理表现。此外,借助Bertopic等主题建模方法,能够从数据中识别出与企业相关的核心ESG议题,并观察这些议题随时间的演进轨迹。该资源以其开放获取特性与连续的时间覆盖,为探究企业可持续性表现的动态变化提供了系统化的数据基础。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值