leetcode 日经贴,Cpp code -Maximal Rectangle

来源:互联网 发布:油田网络宽带客服电话 编辑:程序博客网 时间:2024/06/07 09:35

Maximal Rectangle

class Solution {public:    int maxhis(const vector<int> hist) {        stack<pair<int,int> > st;        int maxarea = 0;        for (int i = 0; i <= hist.size(); ++i) {            int h = i == hist.size() ? -1:hist[i];            if (st.empty() || st.top().first < h) {                st.push(make_pair(h, i));            } else {                int lastp = i;                while (!st.empty() && st.top().first >= h) {                    maxarea = max(maxarea, (i - st.top().second) * st.top().first);                    lastp = st.top().second;                    st.pop();                }                st.push(make_pair(h, lastp));            }        }        return maxarea;    }    int maximalRectangle(vector<vector<char> > &matrix) {        int n = matrix.size();        if (n == 0) return 0;        int m = matrix[0].size();        if (m == 0) return 0;        vector<int> hist(m, 0);        int marea = 0;        for (int i = 0; i < n; ++i) {            for (int j = 0; j < m; ++j) {                hist[j] = matrix[i][j]=='1'? hist[j] + 1: 0;            }            int ca = maxhis(hist);            marea = max(ca, marea);        }        return marea;    }};


0 0