Leetcode Maximal Rectangle

来源:互联网 发布:粒子群算法原理 编辑:程序博客网 时间:2024/05/21 13:50
class Solution {public:    int maximalRectangle(vector<vector<char> > &matrix) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        int ro = matrix.size();        int res = 0;        if(ro<=0)return res;        int c = matrix[0].size();        if(c==0)return res;        vector<int>f(c,0);        vector<int>l(c,-1);        vector<int>r(c,c);        int i,j,k;        for(i=0;i<ro;i++)        {            int leftMost = -1;            for(j=0;j<c;j++)            {                l[j] = max(leftMost,l[j]);                if(matrix[i][j]=='1')                {                    f[j]++;                }                else                {                    l[j]=-1;                    f[j]=0;                    leftMost=j;                }            }            int rightMost=c;            for(j=c-1;j>=0;j--)            {                r[j] = min(rightMost,r[j]);                if(matrix[i][j]=='0')                {                     rightMost = j;                     r[j] = c;                }                res = max(res,f[j]*(r[j] - l[j]-1));            }        }        return res;    }};

原创粉丝点击