leetcode---maximal-rectangle

Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing all ones and return its area.

class Solution {
public:
    int maximalRectangle(vector<vector<char> > &matrix) 
    {
        int n = matrix.size();
        if(n == 0)
            return 0;

        int m = matrix[0].size();
        if(m == 0)
            return 0;

        vector<int> left(m, 0), right(m, m), height(m, 0);
        int ans = 0;

        for(int i=0; iint l = 0, r = m;

            for(int j=0; jif(matrix[i][j] == '1')
                {
                    height[j]++;
                }
                else
                {
                    height[j] = 0;
                }
            }

            for(int j=0; jif(matrix[i][j] == '1')
                {
                    left[j] = max(left[j], l);
                }
                else
                {
                    left[j] = 0;
                    l = j+1;
                }
            }

            for(int j=m-1; j>=0; j--)
            {
                if(matrix[i][j] == '1')
                {
                    right[j] = min(right[j], r);
                }
                else
                {
                    right[j] = m;
                    r = j;
                }
            }

            for(int j=0; jreturn ans;
    }
};

你可能感兴趣的:(题)