首页 > 其他 > 详细

85. Maximal Rectangle

时间:2018-11-24 16:03:45      阅读:133      评论:0      收藏:0      [点我收藏+]
class Solution {
public:
    int maximalRectangle(vector<vector<char>>& matrix) {
        int m = matrix.size(); if (m == 0)  return 0;
        int n = matrix[0].size(); if (n == 0) return 0;
        vector<vector<int>> v(m, vector<int>(n, 0));
        for (int j = 0; j < n; j++)
            v[0][j] = matrix[0][j] == 1;
        for (int i = 1; i < m; i++)
            for (int j = 0; j < n; j++)
            {
                if (matrix[i][j] == 1)
                    v[i][j] = v[i-1][j] + 1;
            }
        int res = 0;
        for (int i = 0; i < m; i++)
            res = max(res, helper(v, i));
        return res;
    }
    int helper(vector<vector<int>>& v, int i) {
        vector<int>& ln = v[i];
        ln.push_back(0);
        stack<int> s;
        int res = 0;
        for (int j = 0; j < ln.size(); j++) {
            while (!s.empty() && ln[j] < ln[s.top()]) {
                int t = s.top();
                s.pop();
                int w = s.empty() ? j : (j - s.top() - 1);
                res = max(res, ln[t] * w);
            }
            s.push(j);
        }
        return res;
    }
};

 

85. Maximal Rectangle

原文:https://www.cnblogs.com/JTechRoad/p/10012263.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!