一天一道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. angularjs中关于跨域设置白名单

    在config中注入$sceDelegateProvider服务使用resourceUrlWhitelist([])方法添加白名单 跨域时将method的属性设置为"jsonp"就 ...

  2. node之querystring模块

    前言 querystring 模块提供了一些实用工具,用于解析与格式化 URL 查询字符串. 一.querystring.parse() 用于将一个查询字符串解析为JS 对象. const query ...

  3. 解决Popup StayOpen=true时,永远置顶的问题

    Popup设置了StayOpen=true时,会置顶显示. 如弹出了Popup后,打开QQ窗口,Popup显示在QQ聊天界面之上. 怎么解决问题? 获取绑定UserControl所在的窗口,窗口层级变 ...

  4. Dubbo介绍和服务架构分析

    Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成.使用zookeeper作为服务的注册中心,对外提供服务 ...

  5. ubuntu15.10 opencv3.1 安装配置codeblocks

    安装codeblocks: sudo add-apt-repository ppa:damien-moore/codeblocks-stable // 添加codeblocks的ppa sudo ap ...

  6. 关于go语言的通道

    1.记一次gorountine导致的泄漏 在项目中使用https://github.com/deckarep/golang-set这个三方包造成了gorountine泄漏.先来看一下这个包的迭代器设置 ...

  7. cassandra 3.x官方文档(5)---探测器

    写在前面 cassandra3.x官方文档的非官方翻译.翻译内容水平全依赖本人英文水平和对cassandra的理解.所以强烈建议阅读英文版cassandra 3.x 官方文档.此文档一半是翻译,一半是 ...

  8. 深入Java虚拟机(3)——安全

    因为网络允许多台计算机共享数据和分布式处理,所以它提供了一条入侵计算机系统的潜在途径,使得其他人可以窃取信息,改变或破坏信息,盗取计算机资源等等.为了解决由网络引起的安全问题,Java体系结构采用了一 ...

  9. SQL Join各种内联外联说明

    Visual Representation of SQL Joins C.L. Moffatt, 3 Feb 2009 CPOL    4.96 (406 votes) Rate this: This ...

  10. Ruby 连接MySQL数据库

    使用Ruby连接数据库的过程还真的是坎坷,于是写点文字记录一下. 简介 Ruby简介 RubyGems简介 包管理之道 比较著名的包管理举例 细说gem 常用的命令 准备 驱动下载 dbi mysql ...