[代码随想录]单调栈

单调栈

739. 每日温度

给定一个整数数组 temperatures ,表示每天的温度,返回一个数组 answer ,其中 answer[i] 是指对于第 i 天,下一个更高温度出现在几天后。如果气温在这之后都不会升高,请在该位置用 0 来代替。

class Solution {
public:
//单调栈需要了解一下
//暴力for时间复杂度n^2,单调栈n
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        vector<int>dp(temperatures.size(),0);
        stack<int>s;
        for(int i = 0;i < temperatures.size();i++){
            while(!s.empty() && temperatures[s.top()] < temperatures[i]){
                dp[s.top()] = i - s.top();
                s.pop();
            }
            s.push(i);
        }
        return dp;
    }
};

496.下一个更大元素 I

nums1 中数字 x下一个更大元素 是指 xnums2 中对应位置 右侧第一个x 大的元素。

给你两个 没有重复元素 的数组 nums1nums2 ,下标从 0 开始计数,其中nums1nums2 的子集。

对于每个 0 <= i < nums1.length ,找出满足 nums1[i] == nums2[j] 的下标 j ,并且在 nums2 确定 nums2[j]下一个更大元素 。如果不存在下一个更大元素,那么本次查询的答案是 -1

返回一个长度为 nums1.length 的数组 ans 作为答案,满足 ans[i] 是如上所述的 下一个更大元素

class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        vector<int>dp(nums1.size(),-1);
        bool flag = false;
        for(int i = 0;i < nums1.size();i++){
            for(int j = 0;j < nums2.size();j++){
                if(!flag && nums2[j] == nums1[i]){
                    flag = true;
                }
                if(flag && nums2[j] > nums1[i]){
                    dp[i] = nums2[j];
                    break;
                }
            }
            flag = false;
        }
        return dp;
    }
};
class Solution {
public:
//暴力简单,单调栈需要思路
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        vector<int>dp(nums1.size(),-1);
        stack<int>s;
        if(nums1.size() == 0 || nums2.size() <= 1)return dp;
        
        //存到哈希树中,方便调用
        unordered_map<int,int>m;
        for(int i = 0;i < nums1.size();i++){
            m[nums1[i]] = i;
        }

        //在num2中查找
        s.push(nums2[0]);
        for(int i = 1;i < nums2.size();i++){
            if(nums2[i] < s.top()){
                s.push(nums2[i]);
            }
            else if(nums2[i] == s.top()){
                s.push(nums2[i]);
            }
            else{
                while(!s.empty() && nums2[i] > s.top()){
                    if(m.count(s.top()) == 1){
                        dp[m[s.top()]] = nums2[i];
                    }
                    s.pop();
                }
                s.push(nums2[i]);
            }
        }
        return dp;
    }
};

503.下一个更大元素II

给定一个循环数组 numsnums[nums.length - 1] 的下一个元素是 nums[0] ),返回 nums 中每个元素的 下一个更大元素

数字 x下一个更大的元素 是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它的下一个更大的数。如果不存在,则输出 -1

class Solution {
public:
    vector<int> nextGreaterElements(vector<int>& nums) {
        vector<int>n(nums);
        for(int i = 0;i < nums.size();i++){
            n.push_back(nums[i]);
        }
        vector<int>dp(n.size(),-1);
        stack<int>s;
        if(nums.size() <= 1)return {-1};
        for(int i = 0;i < n.size();i++){
            while(!s.empty() && n[i] > n[s.top()]){
                dp[s.top()] = n[i];
                s.pop();
            }
            s.push(i);
        }
        dp.resize(nums.size());
        return dp;
    }
};

42. 接雨水

给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

class Solution {
public:
    int trap(vector<int>& height) {
        if(height.size() <= 2)return 0;
        int ret = 0;
        stack<int>s1;//放刚进去数
        stack<int>s2;//放还没有最后执行完的数
        bool flag = false;//表示是否找到左边最大值
        int rightheihght = 0;
        for(int i = 1;i < height.size();i++){
            if(!flag && height[i] > height[i - 1]){//忽略前面递增序列
                rightheihght = i;
                continue;
            }
            else if(!flag && height[i] < height[i - 1]){//找到碗的左边最高
                rightheihght = i - 1;
                flag = true;
                s1.push(height[rightheihght]);
                s1.push(height[i]);
                continue;
            }
            
            if(flag && height[i] <= height[i - 1]){
                s1.push(height[i]);
                continue;
            }

            if(flag && height[i] > height[i - 1] && height[i] < height[rightheihght]){
                while(height[i] > s1.top()){
                    ret += height[i] - s1.top();
                    s1.pop();
                    s2.push(height[i]);
                }
                while(!s2.empty()){
                    ret += height[i] - s2.top();
                    s2.pop();
                    s1.push(height[i]);
                }
                s1.push(height[i]);
                continue;
            }

            if(flag && height[i] >= height[rightheihght]){
                while(!s2.empty()){
                    ret += height[rightheihght] - s2.top();
                    s2.pop();
                }
                while(!s1.empty()){
                    ret += height[rightheihght] - s1.top();
                    s1.pop();
                }
                s1.push(height[i]);
                rightheihght = i;
            }
        }
        return ret;
    }
};

*84.柱状图中最大的矩形

给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。

求在该柱状图中,能够勾勒出来的矩形的最大面积。

class Solution {
public:
//一开始思路错了找山峰了
//只要找上升序列就好了
//每次找到当时最小的与最小与i之间距离
    int largestRectangleArea(vector<int>& heights) {
        if(heights.size() == 1)return heights[0];
        heights.insert(heights.begin(),0);
        heights.push_back(0);
        stack<int>s;
        int ret = 0 ;
        s.push(0);
        for(int i = 1;i < heights.size();i++){
            if(heights[i] >= heights[i - 1]){
                s.push(i);
                continue;
            }
            else{
                while(!s.empty() && heights[i] < heights[s.top()]){
                    int h = heights[s.top()];
                    s.pop();
                    if(!s.empty()){
                        int right = i;
                        int left = s.top();
                        ret = max(ret,h * (right - left - 1));
                    }
                }
                s.push(i);
            }
        }
        return ret;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值