[leetcode]85. Maximal Rectangle

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

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

Example:

Input:
[
  ["1","0","1","0","0"],
  ["1","0","1","1","1"],
  ["1","1","1","1","1"],
  ["1","0","0","1","0"]
]
Output: 6


The DP solution proceeds row by row, starting from the first row. Let the maximal rectangle area at row i and column j be computed by [right(i,j) - left(i,j)]*height(i,j).

All the 3 variables left, right, and height can be determined by the information from previous row, and also information from the current row. So it can be regarded as a DP solution. The transition equations are:

扫描二维码关注公众号,回复: 2724037 查看本文章

left(i,j) = max(left(i-1,j), cur_left), cur_left can be determined from the current row

right(i,j) = min(right(i-1,j), cur_right), cur_right can be determined from the current row

height(i,j) = height(i-1,j) + 1, if matrix[i][j]=='1';

height(i,j) = 0, if matrix[i][j]=='0'


class Solution {
public:
    int maximalRectangle(vector<vector<char>>& matrix) {
        if(matrix.empty() || matrix[0].empty())
            return 0;
        int m=matrix.size(),n=matrix[0].size(), res=0;
        vector<int> left(n,0), right(n, n), height(n,0);

        for(int i=0; i<m; i++) 
        {
            int cur_left=0, cur_right=n; 
            for(int j=0; j<n; j++)  // compute height (can do this from either side)
            {     
                if(matrix[i][j]=='1') 
                    height[j]++; 
                else
                    height[j]=0;
            }
            for(int j=0; j<n; j++)  // compute left (from left to right)
            { 
                if(matrix[i][j]=='1')
                    left[j]=max(left[j],cur_left);
                else
                {
                    left[j]=0; 
                    cur_left=j+1;
                }
            }
        
            for(int j=n-1; j>=0; j--) // compute right (from right to left)
            {
                if(matrix[i][j]=='1') 
                    right[j]=min(right[j],cur_right);
                else 
                {
                    right[j]=n;
                    cur_right=j;
                }    
            }
       
            for(int j=0; j<n; j++)   // compute the area of rectangle (can do this from either side)
                res = max(res,(right[j]-left[j])*height[j]);
        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/xiaocong1990/article/details/80636169