【LeetCode 42】接雨水(单调栈、DP、双指针)

题面:

思路:

能接雨水的点,必然是比两边都低(小)的点。有两种思路,一种是直接计算每个点的最大贡献(也就是每个点在纵向上最多能接多少水),另一种就是计算每个点在横向上有没有贡献。

第一种思路,就需要我们能拿到每个点左右两边最高的高度,这样就能计算每个点在纵向上能接多少水,相当于木桶效应。

第二种思路,对于每个点,则需要判断它左右两边是不是都有比它高的点,每次计算横向上局部能接到水的区域。
官方题解挺好的,具体不再赘述

1. 单调栈

就是第二种思路,每次更新横向上能接到的水。

cpp 复制代码
int trap(vector<int>& height) {
    int ans = 0, n = (int)height.size();
    stack<int> stk;
    for(int i = 0; i < n; ++ i) {
        while(!stk.empty() && height[i] > height[stk.top()]) {
        	// 得到当前低点
            int buttom = stk.top(); stk.pop();
            if(!stk.empty()) {
                int j = stk.top();
                // 如果 height[j] == height[bottom]
                // 就说明 bottom 的左边还没有出现凸出来让bottom能接到水的边边
                ans += (i - j - 1) * (min(height[i], height[j]) - height[buttom]);
            }
        }
        stk.push(i);
    }
    return ans;
}

2. 动态规划

第一种思路,要拿到每个点左右两边的最大高度,就可以考虑线性DP的思想去记录当前点左右两边的最大高度。
l e f t M a x [ i ] = m a x ( l e f t M a x [ i − 1 ] , h e i g h t [ i ] ) r i g h t M a x [ i ] = m a x ( r i g h t M a x [ i + 1 ] , h e i g h t [ i ] ) g o t W a t e r [ i ] = m i n ( l e f t M a x [ i ] , r i g h t M a x [ i ] ) − h e i g h t [ i ] leftMax[i]=max(leftMax[i-1],height[i])\\ rightMax[i]=max(rightMax[i+1],height[i])\\ gotWater[i] = min(leftMax[i],\ rightMax[i])-height[i] leftMax[i]=max(leftMax[i−1],height[i])rightMax[i]=max(rightMax[i+1],height[i])gotWater[i]=min(leftMax[i], rightMax[i])−height[i]

cpp 复制代码
int trap(vector<int>& height) {
    int ans = 0, n = (int)height.size();
    vector<int> leftMax(n), rightMax(n);
    leftMax[0] = height[0]; rightMax[n - 1] = height[n - 1];
    for(int i = 1; i < n; ++ i) leftMax[i] = max(leftMax[i - 1], height[i]);
    for(int i = n - 2; i >= 0; -- i) rightMax[i] = max(rightMax[i + 1], height[i]);

    for(int i = 1; i < n - 1; ++ i)
        ans += min(leftMax[i], rightMax[i]) - height[i];
    return ans;
}

双指针

使用双指针和临时变量优化掉 l e f t M a x leftMax leftMax 和 r i g h t M a x rightMax rightMax 两个数组。

官方题解说:如果 h e i g h t [ l e f t ] < h e i g h t [ r i g h t ] height[left]<height[right] height[left]<height[right],则必有 l e f t M a x < r i g h t M a x leftMax<rightMax leftMax<rightMax。

这主要是因为,我们每次移动的都是 h e i g h t height height 较小的指针,因此如果 l e f t M a x leftMax leftMax 或 r i g h t M a x rightMax rightMax 有更新,则更新了的点 l e f t left left 或 r i g h t right right 在 l e f t M a x leftMax leftMax 或 r i g h t M a x rightMax rightMax 得到新的更新之前会停留一阵子。因此如果 h e i g h t [ l e f t ] < h e i g h t [ r i g h t ] height[left]<height[right] height[left]<height[right],则必有 l e f t M a x < r i g h t M a x leftMax<rightMax leftMax<rightMax。

cpp 复制代码
int trap(vector<int>& height) {
    int ans = 0, n = (int)height.size();
    int left = 0, right = n - 1;
    int leftMax = height[0], rightMax = height[n - 1];
    while(left < right) {
        leftMax = max(leftMax, height[left]);
        rightMax = max(rightMax, height[right]);
        if(height[left] < height[right])
            ans += min(leftMax, rightMax) - height[left ++];
        else
            ans += min(leftMax, rightMax) - height[right --];
    }
    return ans;
}
相关推荐
你好,我叫C小白21 分钟前
C语言 循环结构(1)
c语言·开发语言·算法·while·do...while
寂静山林3 小时前
UVa 10228 A Star not a Tree?
算法
Neverfadeaway3 小时前
【C语言】深入理解函数指针数组应用(4)
c语言·开发语言·算法·回调函数·转移表·c语言实现计算器
Madison-No74 小时前
【C++】探秘vector的底层实现
java·c++·算法
Swift社区4 小时前
LeetCode 401 - 二进制手表
算法·leetcode·ssh
派大星爱吃猫4 小时前
顺序表算法题(LeetCode)
算法·leetcode·职场和发展
liu****4 小时前
8.list的模拟实现
linux·数据结构·c++·算法·list
地平线开发者5 小时前
征程 6 | 征程 6 工具链如何支持 Matmul/Conv 双 int16 输入量化?
算法·自动驾驶
程序员大雄学编程6 小时前
「深度学习笔记4」深度学习优化算法完全指南:从梯度下降到Adam的实战详解
笔记·深度学习·算法·机器学习
小O的算法实验室6 小时前
2022年ASOC SCI2区TOP,基于竞争与合作策略的金字塔粒子群算法PPSO,深度解析+性能实测,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进