单调栈结构

题目:给定一个不含重复值的数组arr,找到每一个i位置左边和右边离i位置最近且值比arr[i]小的位置。返回所有位置的相应信息。
输入:arr={3,4,1,5,6,2,7}
输出:{{-1,2},{0,2},{-1,-1},{2,5},{3,5},{2,-1},{5,-1}}
思路:单调栈

vector<vector<int>>getNearestIndexStack(vector<int>arr){
        vector<vector<int>>res(arr.size(),vector<int>(2));
        stack<int>st;
        for(int i=0;i<arr.size();i++){
            while (!st.empty()&&arr[st.top()]>arr[i])
            {
                int index=st.top();
                st.pop();
                int left_index=st.empty()?-1:st.top();
                res[index].push_back(left_index);
                res[index].push_back(i);
            }
            st.push(i);
        }
        while(!st.empty()){
            int index=st.top();
            st.pop();
            int left_index=st.empty()?-1:st.top();
            res[index].push_back(left_index);
            res[index].push_back(-1);
        }
        return res;

}

leecode84. 柱状图中最大的矩形

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        stack<int>st;
        int maxarea=0;
        for(int i=0;i<heights.size();i++){
            while(!st.empty()&&heights[i]<heights[st.top()]){
                   int index=st.top();
                   st.pop();
                   int left=st.empty()?-1:st.top();
                   maxarea=max(maxarea,(i-left-1)*heights[index]);
            }
            if(!st.empty()&&heights[st.top()]==heights[i]) st.pop();
            st.push(i);
        }
        int left,right=heights.size();
        while(!st.empty()){
            int index=st.top();
            st.pop();
            left=st.empty()?-1:st.top();
            maxarea=max(maxarea,(right-left-1)*heights[index]);
        }
        return maxarea;
    }
};

leecode85. 最大的矩形

class Solution {
public:
     int maximalRectangle(vector<vector<char>>& matrix) {
        if(!matrix.size()) return 0;
        int m=matrix[0].size();
        vector<int> dp(m,0);
        int maxArea = 0;
        for(int i=0;i<matrix.size();++i)
        {
            for(int j=0;j<matrix[0].size();++j)
            {
                dp[j] = matrix[i][j]=='0' ? 0 : dp[j]+1;
            }
            maxArea=max(maxArea,largestRectangleArea(dp));
        }
        return maxArea;
    }
    int largestRectangleArea(vector<int>&heights) {
        stack<int>st;
        int maxarea=0;
        for(int i=0;i<heights.size();i++){
            while(!st.empty()&&heights[i]<heights[st.top()]){
                   int index=st.top();
                   st.pop();
                   int left=st.empty()?-1:st.top();
                   maxarea=max(maxarea,(i-left-1)*heights[index]);
            }
            if(!st.empty()&&heights[st.top()]==heights[i]) st.pop();
            st.push(i);
        }
        int left,right=heights.size();
        while(!st.empty()){
            int index=st.top();
            st.pop();
            left=st.empty()?-1:st.top();
            maxarea=max(maxarea,(right-left-1)*heights[index]);
        }
        return maxarea;
    }
};

402. 移掉K位数字

class Solution {
public:
    string removeKdigits(string num, int k) {
        if (k >= num.size()) return "0";
        string stk;
        for (int i = 0; i < num.size(); i++) {
            while (!stk.empty() && num[i] < stk.back() && k) {
                stk.pop_back();
                k--;
            }
            stk.push_back(num[i]);
        }
        while(k--) {
            stk.pop_back();
        }
        int idx = 0;
        while (idx < stk.size() && stk[idx] == '0') idx++;
        stk = stk.substr(idx, stk.size() - idx);
        return stk.empty() ? "0" : stk;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值