Maximal Rectangle

1.原题链接: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.

2.分析:

(1)学习大神的思路https://leetcode.com/problems/maximal-rectangle/discuss/29054/Share-my-DP-solution;

(2)定义left[i,j]表示第i行第j列的1考虑到前i-1行可以拓展到最左边的坐标,即left[i,j]=t表示matrix[i][j]==1(k=t...j),right[i][j]表示第i行第j列的1考虑到前i-1行可以拓展到最右边的坐标,即right[i,j]=t表示matrix[i][j]==1(k=j...t),height[i,j]表示第i行第j列matrix为1的高度。

3.代码如下:

class Solution {
public:
    int maximalRectangle(vector>& matrix) {
        if(matrix.empty())
            return 0;
        int m=matrix.size(),n=matrix[0].size();
        vector left(n,0),right(n,n-1),height(n,0);
        int cur_left,cur_right,ans=0;
        for(int i=0;i=0;--j){
                if(matrix[i][j]=='1'){
                    right[j]=min(right[j],cur_right);
                }else{
                    right[j]=n-1;
                    cur_right=j-1;
                }
            }
            for(int j=0;j

 

你可能感兴趣的:(算法,leetcode)