码迷,mamicode.com
首页 > 其他好文 > 详细

85. Maximal Rectangle

时间:2018-11-24 16:43:57      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:div   tac   mat   bsp   top   har   public   i++   style   

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

标签:div   tac   mat   bsp   top   har   public   i++   style   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!