首页 > 其他分享 >[42]Trapping Rain Water

[42]Trapping Rain Water

时间:2023-08-17 10:12:32浏览次数:35  
标签:Trapping top 42 height Water Output stack dp

Content

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.

Example 1:
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Explanation: The above elevation map (black section) 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.

Example 2:

Input: height = [4,2,0,3,2,5]
Output: 9

Constraints:

n == height.length
1 <= n <= 2 * 104
0 <= height[i] <= 105

Solution

1. 单调栈

Java

class Solution {

    public int trap(int[] height) {
        // n == height.length
        // 1 <= n <= 2 * 10⁴
        // 0 <= height[i] <= 10⁵
        int n = height.length;
        // 单调递减栈
        int[] stack = new int[n];
        int top = 0;
        // 累加bar
        int[] sums = new int[n];
        sums[0] = height[0];
        for (int i = 1; i < n; i++) {
            sums[i] = sums[i - 1] + height[i];
        }
        // dp[i]表示以下标i所对应的bar作为最右边的bar时,能trap多少雨水。
        int[] dp = new int[n];
        for (int i = 1; i < n; i++) {
            if (height[i - 1] >= height[i]) {
                stack[++top] = i;
                dp[i] = dp[i - 1];
            } else {
                while (top > 0 && height[stack[top]] < height[i]) {
                    top--;
                }
                dp[i] = dp[stack[top]]
                        + Math.min(height[stack[top]], height[i]) * (i - stack[top] - 1)
                        - (sums[i - 1] - sums[stack[top]]);
                if (height[stack[top]] >= height[i]) {
                    stack[++top] = i;
                } else {
                    stack[top] = i;
                }
            }
        }
        return dp[n - 1];
    }
}

标签:Trapping,top,42,height,Water,Output,stack,dp
From: https://www.cnblogs.com/shea24/p/17636866.html

相关文章

  • YU12、I420、YV12、NV12、NV21、YUV420P、YUV420SP、YUV422P、YUV444P的区别
    YUV模型是根据一个亮度(Y分量)和两个色度(UV分量)来定义颜色空间,常见的YUV格式有YUY2、YUYV、YVYU、UYVY、AYUV、Y41P、Y411、Y211、IF09、IYUV、YV12、YVU9、YUV411、YUV420等,其中比较常见的YUV420分为两种:YUV420P和YUV420SP。我们在android平台下使用相机默认图像格式是NV21属......
  • hdu 4291(矩阵快速幂+循环节)
    AShortproblemTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):2487    AcceptedSubmission(s):876ProblemDescriptionAccordingtoaresearch,VIMuserstendtohaveshorterfing......
  • HDU 1423
    GreatestCommonIncreasingSubsequenceTimeLimit:2000/1000MS(Java/Others)  MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):5384  AcceptedSubmission(s):1742ProblemDescriptionThisisaproblemfromZOJ2432.Tomakeiteasyer,you......
  • 4收4发ARINC429模块
    USB2.0Hi-Speed(480Mbits/s)* 发送通道:每路发送通道FIFO大小为511x32bit(CHR32904) 缓存256条发送消息(CHR32904-EX)发送FIFO可设置复位可设置消息间隔,字间隔和发送帧的预定数星发送波特率100Kbps、50Kbps、48Kbps、12.5Kbps、10Kbps可设置标准USB429字格式转换与否......
  • 使用FFmpeg进行yuv420转rgba
    讲解一下将获取到视频数据,进行rgb转码,并且进行相应的缩放操作//存放解码过后的数据unsignedchar*decode_data;intdecode_size=0;/***解码AVFrame中的yuv420数据并且转换为rgba数据**@paramframe需要解码的帧结构*@paramsrc_width需要转换的帧宽度*......
  • 16收16发ARINC429模块
    6通道发送,16通道接收* 发送通道:每路发送通道FIFO大小为:511x32bit(CHR32216/32316),缓存256条发送消息(CHR32216-EX/32316-EX)发送FIFO可设置复位可设置消息间隔,字间隔和发送帧的预定数呈发送波特率100Kbps、50Kbps、48Kbps、12.5Kbps、10Kbps可设置标准ARINC429字格式转换与......
  • CF1422F Boring Queries
    CF1422FBoringQueries题意询问区间\(lcm\),强制在线。题解首先考虑每个质因子对于答案的贡献。对于一个质因子\(p_i\)来说其对于区间\([l,r]\)的贡献是其最高次幂。首先考虑离线做法,扫描线,线段树维护答案。将当前加入的数\(a_i\)分解成\(p_i^{k_i}\),我们有一个暴......
  • 16收16发ARINC429模块
    6通道发送,16通道接收* 发送通道:每路发送通道FIFO大小为:511x32bit(CHR32216/32316),缓存256条发送消息(CHR32216-EX/32316-EX)发送FIFO可设置复位可设置消息间隔,字间隔和发送帧的预定数呈发送波特率100Kbps、50Kbps、48Kbps、12.5Kbps、10Kbps可设置标准ARINC429字格式转换与......
  • 【JavaScript42】axios拦截器
    在前端,我们能看到有些网站会对每次请求都添加加密信息.或者每次返回数据的时候,都有解密逻辑.那此时.你思考.不可能每次请求都要程序员去手动写加密逻辑.axios提供了拦截器.可以对每一个请求进行拦截.并修改请求的内容.拦截器还可以对响应进行拦截.并修改响应的数据.......
  • RISC-V公测平台发布 · 使用YCSB测试SG2042上的MySQL性能
    实验介绍:YCSB(全称为Yahoo!CloudServingBenchmark),该性能测试工具由Java语言编写(在之前的MC文章中也提到过这个,如果没看过的读者可以去看看之前MC那一期),主要用于云端或者服务器端的数据库性能测试工具,其内部涵盖了常见的NoSQL数据库产品,如Cassandra、MongoDB、HBase、Redis等等......