一天一道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. 数组查找算法的C语言 实现-----线性查找和二分查找

    线性查找  Linear Search 用户输入学生学号的成绩 二分查找  Binary Search 要求数据表是已经排好序的 程序存在小的瑕疵

  2. js原生获取元素的css属性

    习惯了用jQuery的css()的方法获取元素的css属性,突然不用jquery了,当要获得元素的css时候,我瞬间停顿了一下,咦?咋获取元素的css值?比如获取元素的width.是这样么?docum ...

  3. 2-学习GPRS_Air202(Air202开发板介绍和下载第一个程序)

    http://www.cnblogs.com/yangfengwu/p/8887933.html 资料链接 链接:https://pan.baidu.com/s/1968t2QITuxoyXlE_Nz ...

  4. Python列表函数&方法

    Python包含以下函数: 序号 函数 1 cmp(list1, list2)比较两个列表的元素 2 len(list)列表元素个数 3 max(list)返回列表元素最大值 4 min(list)返 ...

  5. Python3 基础语法

    编码 默认情况下,Python 3源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串. 当然你也可以为源码文件指定不同的编码: # -*- coding: cp-1252 -*- 标 ...

  6. Python3 注释

    确保对模块, 函数, 方法和行内注释使用正确的风格 Python中的注释有单行注释和多行注释: Python中单行注释以#开头,例如: #!/usr/bin/python3 #coding=utf-8 ...

  7. YAML 在Python中的配置应用

    环境搭建 YAML语法 语法规则 数据结构 列表数组 原子量 YAML应用 案例 load dump 总结 YAML是一个堪比XML,JSON数据格式的更加方便,简洁的,易于人眼阅读的序列化数据格式. ...

  8. jQuery中$(function()与(function($)等的区别详细讲解

    (function($) {-})(jQuery); 这里实际上是匿名函数,如下: function(arg){-} 这就定义了一个匿名函数,参数为arg 而调用函数时,是在函数后面写上括号和实参的, ...

  9. MyBatis批量新增和更新

    之前有开发任务一个接口里面有大量的数据新增和更新操作,导致十分缓慢.使用了批量操作之后速度有明显提升,几乎百倍千倍的速度提升. 博主之前统计过,通过普通接口一次数据库插入大概需要200ms,对于大量新 ...

  10. CentOS7: How to install Desktop Environments on CentOS 7?

    1. Installing GNOME-Desktop: Install GNOME Desktop Environment on here. # yum -y groups install &quo ...