【算法】【單調棧】Leetcode高頻面試題

每日溫度

題目鏈接:https://leetcode-cn.com/problems/daily-temperatures/

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& T) {
        int n = T.size();
        vector<int> res(n, 0);
        stack<int> st;
        //維護一個單調棧
        //當棧頂元素小於當前元素的時候 將當前元素和棧頂元素對應索引的差值放入結果數組中;
        //不滿足,入棧
        for(int i = 0; i < n; i++)
        {
            while(!st.empty() && T[st.top()] < T[i])
            {
                res[st.top()] = i - st.top();
                st.pop();
            }
            st.push(i);
        }
        return res;
    }
};

移掉K位數字

題目鏈接:https://leetcode-cn.com/problems/remove-k-digits/

class Solution {
public:
    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])
            {
                st.pop();
                k--;
            }
            st.push(num[i]);
        }

        string res = "";
        while(!st.empty())
        {
            if(k > 0)
                k--;
            else if(k == 0)
                res += st.top();
            st.pop();
        }

        reverse(res.begin(), res.end());
        int i = 0;
        while(res[i] == '0') i++;
        res = res.substr(i, res.size());
        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;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章