力扣算法刷题Day 49(接雨水)

42 接雨水

题目链接

添加链接描述

思路

首先可以看到,对于一个位置水的高度,取决于右边第一个不小于它的高度的柱子。通过单调栈可以轻松找出右边第一个大于等于自身的柱子。

如何计算水的容量?按高度计算,对于一个池的而言,每一处位置的水的高度 = min(左,右)- 柱子高度,最后相加就是该处池子的水容量。

问题:按列计算难以判断池子的左边界。有多种情况:左边比右边小;当前左边比右边小,但更左边有更大的;以及递减之类。按列会比较难受。因此改用按行计算。

将计算逻辑变动一下即可:当遍历元素大于栈顶元素(递减栈)时,形成凹槽,分别计算凹槽的长和宽,相乘。

文章详解

添加链接描述

cpp 复制代码
class Solution {
public:
    int trap(vector<int>& height) {
        int sum = 0;
        if(height.size() <= 2){
            return sum;
        }
        stack<int> st;
        st.push(0);
        for(int i = 1; i < height.size(); i++)
        {
            if(height[i] < height[st.top()]){
                st.push(i);
            }else if(height[i] == height[st.top()]){
                st.pop();
                st.push(i); //保留右边的
            }else {
                while(!st.empty() && height[st.top()] < height[i]){  //
                    int mid = st.top();
                    st.pop();
                    if(!st.empty()){
                        int h = min(height[i],height[st.top()]) - height[mid];
                        int w = i - st.top() - 1; //注意减一
                        sum += h * w;
                    }
                }
                st.push(i);
            }
        }
        return sum;
    }
};

84 柱状图最大矩形

题目链接

添加链接描述

思路

与接雨水相反,这里要变成右边第一个小于它的,递增栈

文章详解

添加链接描述

cpp 复制代码
class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        stack<int> st;
        heights.insert(heights.begin(), 0); // 数组头部加入元素0
        heights.push_back(0); // 数组尾部加入元素0
        st.push(0);
        int result = 0;
        for (int i = 1; i < heights.size(); i++) {
            while (heights[i] < heights[st.top()]) {
                int mid = st.top();
                st.pop();
                int w = i - st.top() - 1;
                int h = heights[mid];
                result = max(result, w * h);
            }
            st.push(i);
        }
        return result;
    }
};
相关推荐
AI备案指南-满满2 小时前
人工智能拟人化互动服务安全自评估报告的评估要点有哪些?
人工智能·算法·安全·机器人·大模型备案·算法备案
土司大王3 小时前
LeetCode hot100——两两交换链表中的节点
算法·leetcode·职场和发展
大熊背4 小时前
树莓派IspPipeline LSC模块原理详解
算法·lsc·isppipeline·mesh lsc
测试19984 小时前
Selenium 无法定位元素的几种解决方案
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
zander2585 小时前
LeetCode 300. 最长递增子序列
算法·leetcode·深度优先
欧叶冲冲冲5 小时前
Python常见数据结构的CRUD(LeetCode高频版速查)
数据结构·python·leetcode
祖力555 小时前
Linux应用软件编程:目录IO与framebuffer
linux·运维·算法·framebuffer·目录io
名字还没想好☜5 小时前
Go 用 slices/maps 标准库泛型函数:告别手写 Contains、Sort、去重(Go 1.21)
开发语言·后端·算法·golang·go
地平线开发者5 小时前
【模型轻量化专题】深度学习模型为什么需要轻量化
算法