【算法】【单调栈】Leetcode高频面试题

每日温度

题目链接:https://leetcode-cn.com/problems/daily-temperatures/

class Solution {
public:
    //每个元素找到它右边第一个比它大的元素的位置,求它们的距离
    vector<int> dailyTemperatures(vector<int>& T) {
        int n = T.size();
        stack<int> st;
        vector<int> res(n, 0);
        
        //倒着来,变成了找左边第一个比它大的数
        for(int i = n - 1; i >= 0; i--)
        {
            while(!st.empty() && T[st.top()] <= T[i])  st.pop();
            res[i] = st.empty() ? 0 : st.top() - i;
            st.push(i);
        }

        return res;       
    }
};

移掉K位数字

题目链接:https://leetcode-cn.com/problems/remove-k-digits/

class Solution {
public:
    //找到每个数左边第一个比它大的数  移走K个就行了
    string removeKdigits(string num, int k) {
        int n = num.size();
        stack<char> st;
        for(int i = 0; i < n; i++)
        {
            while(!st.empty() && k && st.top() > num[i])
            {
                k--;
                st.pop();
            }
            st.push(num[i]);
        }

        //细节比较多
        //1. k 大于0  当k == 0的时候再往结果中追加
        string res = "";
        while(!st.empty())
        {
            if(k > 0) k--;
            else if(k == 0)  res += st.top();
            st.pop();
        }

        
        reverse(res.begin(), res.end());
        //2. 去除开头的0
        int i = 0;
        while(res[i] == '0') i++;
        res = res.substr(i, res.size() - i + 1);
        if(res == "") return "0";
        return res;
    }
};

柱状图中最大的矩形

题目链接:https://leetcode-cn.com/problems/largest-rectangle-in-histogram/

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        int n = heights.size();

        stack<int> st;
        vector<int> l(n, 0);
        for(int i = 0; i < n; i++)
        {
            //l[i]放的是小于height[i]的第一个位置的索引
            while(!st.empty() && heights[st.top()] >= heights[i]) st.pop();
            l[i] = st.empty() ? -1 : st.top();
            st.push(i);
        }

        st = stack<int>();
        vector<int> r(n, 0);
        for(int i = n - 1; i >= 0; i--)
        {
            //r[i]放的是小于height[i]的倒数第一个位置的索引
            while(!st.empty() && heights[st.top()] >= heights[i]) st.pop();
            r[i] = st.empty() ? -1 : st.top();
            st.push(i);
        }

        int res = 0;
        for(int i = 0; i < n; i++)
        {
            cout << l[i] << " " << r[i] << " ";
            res = max(res, (r[i] - l[i] - 1) * heights[i]);
            cout << res << endl;
        }
        return res;
    }
};

最大矩形

题目链接:https://leetcode-cn.com/problems/maximal-rectangle/

class Solution {
public:
    int maximalRectangle(vector<vector<char>>& matrix) {
        if(matrix.empty()) return 0;
        int n = matrix.size(), m = matrix[0].size();
        vector<vector<int>> height(n, vector<int>(m, 0));
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
            {
                if(matrix[i][j] == '1') height[i][j] = 1;
                if(i >= 1 && matrix[i][j] == '1') height[i][j] = height[i - 1][j] + 1;
            }

        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
                cout << height[i][j] << " ";
            cout << endl;
        }
        int res = 0;
        for(int i = 0; i < n; i++)
        {
            res = max(res, func(height[i]));
        }
        return res;
    }

    int func(vector<int> h)
    {
        int n = h.size();

        stack<int> st;
        vector<int> l(n, 0);
        for(int i = 0; i < n; i++)
        {
            while(!st.empty() && h[st.top()] >= h[i]) st.pop();
            l[i] = st.empty() ? -1 : st.top();
            st.push(i);
        }

        st = stack<int>();
        vector<int> r(n, 0);
        for(int i = n - 1; i >= 0; i--)
        {
            while(!st.empty() && h[st.top()] >= h[i]) st.pop();
            r[i] = st.empty() ? n : st.top();
            st.push(i);
        }
        
        int res = 0;
        for(int i = 0; i < n; i++)
            res = max(res, (r[i] - l[i] - 1) * h[i]);
        return res;
    }
};
原文地址:https://www.cnblogs.com/Trevo/p/13568321.html