Maximal Rectangle

本文介绍了一种寻找二维二进制矩阵中全为1的最大矩形区域的方法,提供了两种算法实现,分别为O(n*m^2)和更优的O(n^2)复杂度,并通过实例解释了算法的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


方法一:O(n*m^2),计算每一列中的1字组成的竖线能向左扩展多长。


class Solution {
public:
    int maximalRectangle(vector<vector<char> > &matrix) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int m = matrix.size();
        if(m == 0) return 0;
        int n = matrix[0].size();
        if(n == 0) return 0;
        
        vector<int> vec(m,0);
        vector<vector<int>> area(m,vec);
        int maxArea = 0;
            
        for(int i = 0;i<m;i++)
        { 
            for(int j = i;j< m;j++)
            {
                if(matrix[j][0] == '0') break;
                area[i][j] = j-i+1;
                if(area[i][j] > maxArea) maxArea = area[i][j];
            }
        }
        
        for(int k = 1;k<n;k++)
        {
            for(int i = 0;i<m;i++)
            {
                int flag = 0;
                for(int j = i;j< m;j++)
                {
                    if(flag == 1){
                        area[i][j] = 0;
                    }else if(matrix[j][k] == '0') {
                        flag = 1;
                        area[i][j] = 0;
                    }else{
                        area[i][j] += j-i+1;
                        if(area[i][j] > maxArea) maxArea = area[i][j];
                    }
                }
            }
        }

        return maxArea;
    }
};

244 milli secs.


方法二:一种更好的实现,O(n^2)复杂度。

class Solution {
public:
    int maximalRectangle(vector<vector<char> > &matrix) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int m = matrix.size();
        if(m == 0) return 0;
        int n = matrix[0].size();
        if(n == 0) return 0;
        
        vector<int> H(n,0);
        vector<int> L(n,-1);
        vector<int> R(n,n);
        int maxArea = 0;
        
        for(int i = 0;i < m;i++)
        {
            int left = -1;
            int right = n;
            for(int j = 0;j < n;j++)
            {
                if(matrix[i][j] == '1'){
                    H[j] += 1;
                    L[j] = max(L[j],left);
                }else{
                    H[j] = 0;
                    L[j] = -1;
                    left = j;
                }
            }
            
            for(int j = n-1;j >= 0;j--)
            {
                if(matrix[i][j] == '0'){
                    right = j;
                    R[j] = n;
                }else{
                    R[j] = min(right,R[j]);
                    int area = H[j] * (R[j] - L[j]-1);
                    maxArea = max(maxArea,area);
                }
            }
        }
    
        return maxArea;
        
    }
};

44 milli secs.

A boundary of the maximal rectangle must contain one or more 0's or it is the boundary of the matrix.

Fig. 1

Fig. 1: The green rectangle cannot grow any larger since the boundary either contains 0’s or is the boundary of the matrix.

In this problem, we define a "hanging line" to be a subcolumn that contains all 1's except its upper boundary. The topmost can be either a zero or the matrix boundary. Some examples are circled in the same matrix below. A general example is given in Fig.3.

Fig. 2

Fig. 2: Three hanging lines are circled above.

Fig. 3

Fig. 3: The solid circles are 0's while the lines contain 1's only.

Every point is bijective to a hanging line (From this point to the top nearest 0 or to the matrix boundary). To get a maximal rectangle from a hanging line, we move the line to the left and right as far as possible. To achieve this goal, we use dynamic programming to calculate the leftmost and the rightmost positions that the line can reach to. We also need to know the length of the hanging line to calculate the area. Specifically,

Case 1 -- matrix(i, j) = 1
H(i, j) = H(i-1, j) + 1
L(i, j) = max( L(i-1, j), the position of the left  nearest 0 in this row )
R(i, j) = min( R(i-1, j), the position of the right nearest 0 in this row )

Case 2 -- matrix(i, j) = 0
H(i, j) = 0
L(i, j) = 0
R(i, j) = n
详见 http://discuss.leetcode.com/questions/260/maximal-rectangle




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值