Maximal Rectangle [leetcode]

第一种方法是利用DP,时间复杂度是 O(m * m * n)

dp(i,j):矩阵中同一行以(i,j)结尾的全部为1的最长子串长度

代码如下:

    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<vector<int>> dp(m, vector<int>(n));
        int maxArea = 0;
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (matrix[i][j] == '0') continue;
                dp[i][j] = (j == 0 ? 1 : dp[i][j - 1] + 1);
                int minX = dp[i][j];
                for (int k = 1; k <= i + 1; k++)
                {
                    minX = min(minX, dp[i - k + 1][j]);
                    maxArea = max(maxArea, minX * k);
                }
            }
        }
        return maxArea;
    }

第二种方法:

来自https://oj.leetcode.com/discuss/5198/a-o-n-2-solution-based-on-largest-rectangle-in-histogram

其实这里和 Largest Rectangle in Histogram是类似的,

之前的dp(i,j)保存以第i行,第j列结尾的,同一行中连续1的个数;那么这里我们用一个数组x,使x[j]保存当前行第j列中的连续1的个数。之后按行遍历,每一行都按Largest Rectangle in Histogram的算法处理一遍

代码如下:复杂度为O(m*n)

    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> height(n);
        int maxArea = 0;
        for (int i = 0; i < m; i++)
        {
            vector<int> index;
            for (int j = 0; j < n; j++)
            {
                if (matrix[i][j] == '0') height[j] = 0;
                else height[j] ++;
                while (index.size() && height[j] <= height[index.back()])
                    maxArea = max(getArea(height, index, j), maxArea);
                index.push_back(j);
            }
            while (index.size())
                maxArea = max(getArea(height, index, height.size()), maxArea);
        }
        return maxArea;
    }

    int getArea(vector<int> &height, vector<int>& index, int start)
    {
        int areaH = height[index.back()];
        index.pop_back();
        int end = index.empty() ? -1 : index.back();
        return (start - end - 1) * areaH;
    }

第三种方法:

利用极值http://hi.baidu.com/mzry1992/item/030f9740e0475ef7dc0f6cba

H[i][j] = 0                    matrix[i][j] = ‘0‘

H[i-1][j] + 1     matrix[i][j] = ‘1‘

L[i][j] = max{L[i-1][j], 第i行左边第一个‘0‘的位置+1}

R[i][j] = min{R[i-1][j], 第i行右边第一个‘0‘的位置-1}

maxArea = max{maxArea, H[i][j] * (R[i][j] - L[i][j] + 1)}

由于H,L,R均只用到i-1,j的内容,可以将空间进一步压缩成为O(N)的

代码如下:

    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> h(n);
        vector<int> l(n);
        vector<int> r(n, n - 1);
        int maxArea = 0, maxL = 0, minR = m - 1;
        for (int i = 0; i < m; i++)
        {
            maxL = 0, minR = n-1;
            for (int j = 0; j < n; j++)
            {
                if (matrix[i][j] == '0')
                {
                    h[j] = 0;
                    l[j] = 0;
                    r[j] = n - 1;
                    maxL = j + 1;
                }
                else
                {
                    h[j]++;
                    l[j] = max(l[j], maxL);
                }
            }

            for (int j = n - 1; j >= 0; j--)
            {
                if (matrix[i][j] == '0')
                {
                    r[j] = n - 1;
                    minR = j - 1;
                }
                else
                {
                    r[j] = min(r[j], minR);
                    maxArea = max(maxArea, h[j] * (r[j] - l[j] + 1));
                }
            }
        }
        return maxArea;
    }
时间: 2024-10-10 21:47:42

Maximal Rectangle [leetcode]的相关文章

Maximal Rectangle leetcode java

题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 题解: 这道题可以应用之前解过的Largetst Rectangle in Histogram一题辅助解决.解决方法是: 按照每一行计算列中有1的个数,作为高度,当遇见0时,这一列高度就为0.然后对每一行计算 Largetst Rectangle in H

LeetCode: Maximal Rectangle

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. 地址:https://oj.leetcode.com/problems/maximal-rectangle/ 算法:要解决这道题,得利用Largest Rectangle in Histogram这道题的解法

LeetCode: Maximal Rectangle [085]

[题目] Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. [题意] 给定一个由0和1填充的二维矩阵,找一个全是1的最大矩形 [思路] 扫描二维矩阵,凡是扫到值为1的块时候,以当前块为矩形的左上角区块拓展,找最大矩阵. 先找出以每个"1"区块为左上角区块的最大矩形,然后求出最大全局的最大矩形. 以下图为

【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. 题解,很巧妙的一道题,对于一个0-1矩阵,它的每一行及以上都可以看作一个直方图(如下图所示),利用Largest Rectangle in Histogram的方法,可以在O(n)的时间搜索出这一行及以上的直方图中面积最大的矩形,对矩阵的每一行依次做这个操作,就可

[LeetCode] Maximal Rectangle(good)

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 参考“Largest Rectangle in Histogram”这个题的解法,思想差不多一样,只是用h向量表示Rectangle中此元素中第一行到本行的高度,非常妙的算法: class Solution { public: int maximalRectang

LeetCode: Maximal Rectangle 解题报告

Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. Show TagsHave you met this question in a real interview? Yes  NoDiscussSOLUTION 1: 1 public class Solution { 2 publ

Leetcode:Maximal Rectangle 最大全1子矩阵

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. 解题分析: 联想到 最大矩形面积 这一题,可以在O(n)时间内求出 最大的矩形面积 如果我们把每一行看成x坐标,那高度就是从那一行开始往上数的1的个数. 利用 最大矩形面积 的方法,在O(n2)时间内就可以求出每一行形成的“柱状

【leetcode】Maximal Rectangle

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. 使用dpHeight[]数组来记录到第i行为止,第j个位置垂直连续包含多少个1(包括matrxi[i][j]).如: 1 0 1 1 0 1 1 0 1 0 0 1 1 1 1 有如下结果: 第1行: dpHeight[] =

LeetCode开心刷题三十二天——85. Maximal Rectangle

85. Maximal Rectangle Hard 161653FavoriteShare 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"],