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

85. Maximal Rectangle

时间:2018-05-20 10:48:25      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:desc   har   ==   solution   for   push   max   return   int   

https://leetcode.com/problems/maximal-rectangle/description/

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<int> v(n, 0);
        int res = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] == 0)
                    v[j] = 0;
                else
                    v[j]++;
            }
            res = max(res, helper(v));
        }
        return res;
    }
    int helper(const vector<int>& v) {
        stack<int> s;
        int res = 0;
        for (int i = 0; i <= v.size(); i++) {
            int cur = i == v.size() ? 0 : v[i];
            while (!s.empty() && cur < v[s.top()]) {
                int idx = s.top();
                s.pop();
                int w = s.empty() ? i : i - s.top() - 1;
                res = max(res, w * v[idx]);
            }
            s.push(i);
        }
        return res;
    }
};

 

85. Maximal Rectangle

标签:desc   har   ==   solution   for   push   max   return   int   

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

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