代码随想录算法训练营 | 单调栈

739. 每日温度 - 力扣(LeetCode)//中等。之前哪道题讲过单调栈来着。忘记了。反正这题懂单调栈就能a,没什么难度

复制代码
class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        vector<int> answer(temperatures.size(),0);
        vector<int> st;
        st.push_back(0);
        for(int i=1;i<temperatures.size();i++){
            while(!st.empty()&&temperatures[i]>temperatures[st.back()]){
                answer[st.back()]=i-st.back();
                st.pop_back();
            }
            st.push_back(i);
        }
        return answer;
    }
};

496. 下一个更大元素 I - 力扣(LeetCode)//简单。如果要用单调栈的话,实现起来并不算特别简单。。

复制代码
class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        vector<int> ans(nums1.size(),-1);
        unordered_map<int,int> map;
        stack<int> st;
        for(int i=0;i<nums1.size();i++){
            map[nums1[i]]=i;
        }
        st.push(nums2[0]);
        for(int i=1;i<nums2.size();i++){    
            while(!st.empty()&&nums2[i]>st.top()){
                if(map.find(st.top())!=map.end()){
                    ans[map[st.top()]]=nums2[i];
                }
                st.pop();
            }
            st.push(nums2[i]);      
        }
        return ans; 
    }
};

503. 下一个更大元素 II - 力扣(LeetCode)//中等。懒得想。直接跑两次,然后用余数区分一下即可。写完一看思路差不多。

复制代码
class Solution {
public:
    vector<int> nextGreaterElements(vector<int>& nums) {
        vector<int> ans(nums.size(),-1);int i=1;
        stack<int> st;
        st.push(0);
        while(i<2*nums.size()){
            while(!st.empty()&&nums[i%nums.size()]>nums[st.top()]&&ans[st.top()]==-1){
                ans[st.top()]=nums[i%nums.size()];
                st.pop();
            }
            if(ans[i%nums.size()]==-1)
            st.push(i%nums.size());
            i++;
        }
        return ans;
    }
};

42. 接雨水 - 力扣(LeetCode)//困难。难点在于知道单调栈之后如何妥当的计算面积

复制代码
class Solution {
public:
    int trap(vector<int>& height) {
        int count=0;
        vector<int> st;
        st.reserve(height.size());
        st.push_back(0);
        for(int i=1;i<height.size();i++){
            while(!st.empty()&&height[st.back()]<height[i]){//满足条件说明出现凹槽
                int mintemp=st.back();
                st.pop_back();
                if(!st.empty()){
                    count+=((min(height[st.back()],height[i])-height[mintemp])*(i-st.back()-1));
                }
            }
            st.push_back(i);
        }
        return count;
    }
};

84. 柱状图中最大的矩形 - 力扣(LeetCode)//困难。虽然和接雨水有异曲同工之妙,但是边界处理比雨水要复杂一点。

复制代码
class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        heights.insert(heights.begin(),0);
        heights.push_back(0);
        int count=0;
        stack<int> st;
        st.push(0);
        for(int i=1;i<heights.size();i++){
            while(!st.empty()&&heights[st.top()]>heights[i]){
                int temp=st.top();
                st.pop();
                if(!st.empty())
                count=max(count,(i-st.top()-1)*heights[temp]);
            }
            st.push(i);
        } 
        return count;   
    }
};
相关推荐
moshuying6 分钟前
块扩散 + GDN 改造:Clover 把「并行生成」塞进了 Agent 的长上下文里
前端·算法
没落之王19 分钟前
历史系统建模:宋初太祖太宗「再立天庭」秩序重构推演
人工智能·算法·重构
INGNIGHT1 小时前
91 · 最小调整代价(序列dp)
数据结构·算法·leetcode
moshuying1 小时前
扩散模型开始写代码、开网页了:350 亿参数,速度比 Jev 快一倍
前端·算法
小超人诞生日记1 小时前
C++ 异常机制详解:从抛出捕获到异常安全
c++·算法
labuladong2 小时前
机器学习&神经网络 系列教程上线
神经网络·算法·机器学习
xiangyun612 小时前
C++ 手撕 shared_ptr:引用计数与控制块到底长什么样?
开发语言·c++·算法·面试·stl
学习论之费曼学习法2 小时前
无人值守AI内容矩阵实战(8):标题 A/B 实验的 6 个工程实现细节,隔日对比与自动判胜
人工智能·python·算法·矩阵·自动化·a/b测试·内容矩阵
Carl_奕然3 小时前
【机器视觉】最强算法之:3D高斯泼溅(3D Gaussian Splatting)
人工智能·python·算法·目标检测·计算机视觉·3d
w_zero_one3 小时前
算法--桶思想
java·开发语言·算法