力扣85题最大矩形

力扣85题最大矩形

题目描述

给定一个仅包含 01 、大小为 rows x cols 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。

输入输出样例

在这里插入图片描述

输入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
输出:6
解释:最大矩形如上图所示。
输入:matrix = []
输出:0
输入:matrix = [["0"]]
输出:0
输入:matrix = [["1"]]
输出:1

解法一使用暴力解法

   int maximalRectangle2(vector<vector<char>>&matrix)
    {
        int rlength=matrix.size();
        int clength=matrix[0].size();
        if(rlength==0)
        {
            return 0;
        }

        vector<vector<int>>width(rlength,vector<int>(clength));
        int maxArea=0;

        //遍历每一行
        for(int row=0;row<rlength;row++)
        {
            for(int col=0;col<clength;col++)
            {
                if(matrix[row][col]=='1')
                {
                    //当前值保存当前连续一行1的值
                    if(col==0)
                    {
                        width[row][col]=1;
                    }
                    else
                    {
                        width[row][col]=width[row][col-1]+1;
                    }
                }
                else{
                    width[row][col]=0;
                }

                //记录所有行中最小的数
                //以当前列值的最小值作为宽
                int minWidth=width[row][col];
                for(int upRow=row;upRow>=0;upRow--)
                {
                    int height=row-upRow+1;
                    minWidth=min(minWidth,width[upRow][col]);
                    maxArea=max(maxArea,height*minWidth);
                }
            }
        }
            return maxArea;

    }

解法2,利用堆栈求解

/结合上一题求面积最大值的思路
    int maximalRectangle(vector<vector<char>>&matrix)
    {
        int rlength=matrix.size();
        int clength=matrix[0].size();

        vector<vector<int>>res(rlength,vector<int>(clength));

        for(int i=0;i<rlength;i++)
        {
            for(int j=0;j<clength;j++)
            {
                if(matrix[i][j]=='1')
                {
                    res[i][j]=1;
                }
                else{
                    res[i][j]=0;
                }
            }
        }
        for(int i=0;i<rlength-1;i++)
        {
            for(int j=0;j<clength;j++)
            {
                if(res[i+1][j])
                {
                    res[i+1][j]=res[i+1][j]+res[i][j];
                }
            }
        }

        int maxArea=0;

        for(int i=0;i<rlength;i++)
        {
            int temp=largestRectangleArea(res[i]);
            maxArea=max(temp,maxArea);
        }

        return maxArea;

    }

    int largestRectangleArea(vector<int>&height)
    {
        int length=height.size();
        if(length==0)
        {
            return 0;
        }
        else if(length==1)
        {
            return height[0];
        }

        //创建数组并保存哨兵
       vector<int>tempList(length+2);
        for(int i=0;i<length;i++)
        {
            tempList[i+1]=height[i];
        }
        tempList[0]=0;
        tempList[length+1]=0;
        int maxArea=0;

        length+=2;
        height=tempList;

        //建立堆栈,将哨兵先入堆栈
        stack<int>stk;
        stk.push(0);
        for(int i=1;i<length;i++)
        {
            while(height[stk.top()]>height[i])
            {
                int high=height[stk.top()];
                stk.pop();
                int width=i-stk.top()-1;
                maxArea=max(maxArea,high*width);
            }
            stk.push(i);
        }
        return maxArea;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值