代码随想录day58 59 60单调栈集合 顺便加一个单调队列

739每日温度

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& nums) {
        int n = nums.size();
        vector<int> res(n);
        stack<int>s;
        for (int i = n - 1; i >= 0; i--) 
        {
            while (!s.empty() && nums[s.top()] <= nums[i]) 
            {
                s.pop();
            }
            res[i] = s.empty() ? 0 : (s.top()-i);
            s.push(i);
        }
    return res;
    }
};

496下一个更大元素

class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        int n = nums2.size();
        int res=0;
        stack<int>s;
        unordered_map<int,int>ump;
        for (int i = n - 1; i >= 0; i--) 
        {
            while (!s.empty() && s.top() <= nums2[i]) 
            {
                s.pop();
            }
            res = s.empty() ? -1 : s.top();
            s.push(nums2[i]);
            ump[nums2[i]]=res;
        }
        int m=nums1.size();
        vector<int>ans(m,0);
        for(int i=0;i<m;i++)
        {
            ans[i]=ump[nums1[i]];
        }
        return ans;
    }
};

503下一个更大元素2

class Solution {
public:
    vector<int> nextGreaterElements(vector<int>& nums) 
    {
        int n=nums.size();
        vector<int>res(n);
        stack<int>s;
        for(int i=2*n-1;i>=0;i--)
        {
            while(!s.empty()&&s.top()<=nums[i%n])
            {
                s.pop();
            }
            res[i%n]=s.empty()?-1:s.top();
            s.push(nums[i%n]);
        }
        return res;
    }
};

42接雨水

class Solution {
public:
    stack<int>st;
    int res=0;
    int trap(vector<int>& h) 
    {
        for(int i=0;i<h.size();i++)
        {
            while(!st.empty()&&h[st.top()]<h[i])
            {
                int b = st.top();
                //保留住栈顶元素 这是装雨容器的底h[b]
                while(!st.empty()&&h[st.top()]==h[b])
                {
                    st.pop();
                }
                //不断去除重复元素  找到栈里第一个比底部更大的元素 h[st.top()]  左边界
                //  h[i]右边界
                if (!st.empty()) 
                {
                    res += (min(h[st.top()], h[i]) - h[b]) * (i - st.top() - 1);
                }
            }
            st.push(i);
        }
        return res;
    }
    
};

class Solution {
public:
    int trap(vector<int>& height) 
    {
        int left = 0, right = height.size() - 1;
        int l_max = 0, r_max = 0;

        int res = 0;
        while (left < right) 
        {
            l_max = max(l_max, height[left]);
            r_max = max(r_max, height[right]);

            if (l_max < r_max) 
            {
                res += l_max - height[left];
                left++;
            } 
            else {
                res += r_max - height[right];
                right--;
            }
        }
        return res;
    }
};

84柱状图中的最大矩形

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) 
    {
        stack<int>st;
        int result = 0;
        heights.insert(heights.begin(), 0); 
        heights.push_back(0);
        st.push(0);
        for(int i=1;i<heights.size();i++)
        {
            while(!st.empty()&&heights[i]<heights[st.top()])
            {
                int m=st.top();
                st.pop();
                int h=heights[m];
                result=max(result,(i-st.top()-1)*h);
            }
            st.push(i);
        }
        return result;
    }
};

11盛水最多的容器

class Solution {
public:
    int maxArea(vector<int>& height) 
    {
        int left = 0, right = height.size() - 1;
        int res = 0;
        while (left < right) 
        {
            int cur_area = min(height[left], height[right]) * (right - left);
            res = max(res, cur_area);

            if (height[left] < height[right]) 
            {
                left++;
            } 
            else 
            {
                right--;
            }
        }
        return res;
    }
};

239滑动窗口最大值

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) 
    {
        int n=nums.size();
        vector<int>res;
        deque<int>q;
        for(int i=0;i<n;i++)
        {
            if(!q.empty()&&i-k+1>q.front()) q.pop_front();
            while(!q.empty()&&nums[q.back()]<nums[i])
            {
                q.pop_back();
            }
            q.push_back(i);
            if (i>= k-1) res.push_back(nums[que.front()]);
        }
        return res;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值