85. Maximal Rectangle

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;
    }
};

 

posted @ 2018-05-20 04:32  JTechRoad  阅读(99)  评论(0编辑  收藏  举报