一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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

(二)解题

题目大意:给定一个二值矩阵,计算矩阵里面包含1的所有子矩阵的最大面积。

例如:

1000

1101

1111

1111

构成的矩形有第0列面积4,第2,3行面积8等等。

那么我们思考一下如何找到这些矩形,并求出他们的面积呢?

对于(i,j)我们已它为矩形的左上角,那么需要找出,它的向右延伸有多少个1,它向下延伸有多少个1,再就是每一行向右延伸有多少个1,如(1,0)向右延伸2个1,向下延伸3个1,那么(2,0),(3,0)向右延伸均大于2,故这个矩形的面积为6。

class Solution {
public:
    int maximalRectangle(vector<vector<char>>& matrix) {
        if (matrix.empty()) return 0;
        int row = matrix.size();
        int col = matrix[0].size();
        vector<vector<int>> sameRowNum;//存放当前点向下延伸多少个1
        vector<vector<int>> sameColNum;//存放当前点向右延伸多少个1
        for (int i = 0; i < row; i++)
        {
            vector<int> tmp(col, 0);
            sameColNum.push_back(tmp);
            sameRowNum.push_back(tmp);
        }
        for (int i = 0; i < row; i++)//计算sameRowNum
        {
            for (int j = 0; j < col; )
            {
                if (matrix[i][j] == '1') {
                    int t = j;
                    int count = 0;
                    while (t < col&&matrix[i][t++] == '1') count++;//计算多少个1
                    while (j<t) sameColNum[i][j++] = count--;//赋值
                }
                else j++;
            }
        }
        for (int i = 0; i < col; i++)
        {
            for (int j = 0; j < row; )
            {
                if (matrix[j][i] == '1') {
                    int t = j;
                    int count = 0;
                    while (t < row&&matrix[t++][i] == '1') count++;//计算多少个1
                    while (j<t) sameRowNum[j++][i] = count--;//赋值
                }
                else j++;
            }
        }
        int maxArea = 0;
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col;j++ )
            {
                if (matrix[i][j]=='1')
                {
                    int t = i;
                    int mincol = sameColNum[i][j];
                    while (t < row&&t < i + sameRowNum[i][j]) {
                        mincol = mincol < sameColNum[t][j] ? mincol : sameColNum[t][j];//依次计算矩形面积
                        int area = mincol*(t-i+1);
                        maxArea = max(maxArea, area);//求最大值
                        t++;
                    }
                }
            }
        }
        return maxArea;
    }
};

【一天一道LeetCode】#85. Maximal Rectangle的更多相关文章

  1. 求解最大矩形面积 — leetcode 85. Maximal Rectangle

    之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...

  2. LeetCode (85): Maximal Rectangle [含84题分析]

    链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...

  3. [LeetCode] 85. Maximal Rectangle 最大矩形

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

  4. leetcode[85] Maximal Rectangle

    给定一个只含0和1的数组,求含1的最大矩形面积. Given a 2D binary matrix filled with 0's and 1's, find the largest rectangl ...

  5. leetCode 85.Maximal Rectangle (最大矩阵) 解题思路和方法

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

  6. 85. Maximal Rectangle

    85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...

  7. 刷题85. Maximal Rectangle

    一.题目说明 题目,85. Maximal Rectangle,计算只包含1的最大矩阵的面积.难度是Hard! 二.我的解答 看到这个题目,我首先想到的是dp,用dp[i][j]表示第i行第j列元素向 ...

  8. 【LeetCode】85. Maximal Rectangle

    Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...

  9. 【leetcode】85. Maximal Rectangle(单调栈)

    Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...

随机推荐

  1. bootstrap的模态框

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. Python学习札记-eval函数

    eval()函数 eval()官方文档里面给出来的功能解释是:将字符串string对象转化为有效的表达式参与求值运算返回计算结果 语法上:调用的是:eval(expression,globals=No ...

  3. thinkphp零碎小知识

    在使用thinkphp搭建后台的时候,有很多的参数需要去配置,有的记不住还要去查找,这里把一些基本的参数整理一下,还有些零碎的知识点,与大家共勉,希望能方便大家. 友情提示:这些配置是 thinkph ...

  4. Python中迭代输出(index,value)的几种方法

    需求如下:迭代输出序列的索引(index)和索引值(value). 1.创建测试列表: >>> lst = [1,2,3,4,5] 2.实现方法如下: #方法1:range()+le ...

  5. vmware 12中安装MAC OS X Lion 10.7

    下载并安装vmware.    下载并安装MAC补丁.    创建虚拟机.    设置ISO文件.    开启虚拟机.    安装vmware tools. 1. 下载并安装vmware.我是直接在腾 ...

  6. 【事务】<查询不到同一调用方法其它事务提交的更新>解决方案

    最近遇到一个很棘手的问题,至今也解释不清楚原因,不过已经找到了解决方案. 先来看看Propagation属性的值含义,@Transactional中Propagation属性有7个选项可供选择: Pr ...

  7. 【Unity Shader】自定义材质面板的小技巧

    写在前面 之前遇到过一些朋友问怎么在材质面板里定义类似于bool这种变量,控制一些代码的执行.我们当然可以写一个C#文件来自定义材质面板,就像Unity为Standard Shader写材质面板一样( ...

  8. MacOS和iOS开发中异步调用与多线程的区别

    很多童鞋可能对Apple开发中的异步调用和多线程的区别不是太清楚,这里本猫将用一些简单的示例来展示一下它们到底直观上有神马不同. 首先异步调用可以在同一个线程中,也可以在多个不同的线程中.每个线程都有 ...

  9. FLAnimatedImage -ios gif图片加载框架介绍

    简介 FLAnimatedImage 是 Flipboard 团队开发的在它们 App 中渲染 GIF 图片使用的库. 后来 Flipboard 将 FLAnimatedImage 开源出来供大家使用 ...

  10. 从源码安装git

    蛋疼的阿里云,git版本居然才1.9.只能手动安装了. 预装 apt-get update apt-get --yes install libcurl4-gnutls-dev libexpat1-de ...