【一天一道LeetCode】#85. Maximal Rectangle
一天一道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的更多相关文章
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
- [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 ...
- leetcode[85] Maximal Rectangle
给定一个只含0和1的数组,求含1的最大矩形面积. Given a 2D binary matrix filled with 0's and 1's, find the largest rectangl ...
- leetCode 85.Maximal Rectangle (最大矩阵) 解题思路和方法
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- 85. Maximal Rectangle
85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...
- 刷题85. Maximal Rectangle
一.题目说明 题目,85. Maximal Rectangle,计算只包含1的最大矩阵的面积.难度是Hard! 二.我的解答 看到这个题目,我首先想到的是dp,用dp[i][j]表示第i行第j列元素向 ...
- 【LeetCode】85. Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- 【leetcode】85. Maximal Rectangle(单调栈)
Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...
随机推荐
- 对闭包的理解(closure)
什么是闭包: 当你声明一个局部变量时,这个局部变量有作用域,通常局部变量值只存在于你定义的Block or Function中: function() { var a = 1; console.log ...
- 转:rabbitmq——用户管理
原文:http://my.oschina.net/hncscwc/blog/262246?p={{currentPage-1}} 安装最新版本的rabbitmq(3.3.1),并启用managemen ...
- UCSC下载ENCODE数据
ENCODE数据库用于存放基因组原件,所有的测序数据(原始数据以及每一步处理后的数据以及最终的结果)都是开放下载的.假如说去官网下载的话会比较麻烦,这里可以通过UCSC的数据库下载(真的是神器啊)!下 ...
- MongoDB 条件操作符
描述 条件操作符用于比较两个表达式并从mongoDB集合中获取数据. 在本章节中,我们将讨论如何在MongoDB中使用条件操作符. MongoDB中条件操作符有: (>) 大于 - $gt (& ...
- Oracle数据库常用命令记录
1.Sql建表 CREATE TABLE AAABBBCCCDDD( ID ) primary key, AAAAAAAA ) not NULL, BBBBBBBB ), CCCCCCCC ), DD ...
- 实验与作业(Python)-文件操作
1.CSV文件的处理 下载-身份证号文件 导入: 读入"身份证号.txt",然后打印出来.注意:是否多打了一行,为什么? 读入"身份证号.txt",然后存储到& ...
- Spring动态切换多数据源解决方案
Spring动态配置多数据源,即在大型应用中对数据进行切分,并且采用多个数据库实例进行管理,这样可以有效提高系统的水平伸缩性.而这样的方案就会不同于常见的单一数据实例的方案,这就要程序在运行时根据当时 ...
- mysql 数据类型别名参考
To facilitate the use of code written for SQL implementations from other vendors, MySQL maps data ty ...
- log file sync 因为数据线有问题而造成高等侍的表现
这是3月份某客户的情况,原因是服务器硬件故障后进行更换之后,业务翻译偶尔出现提交缓慢的情况.我们先来看下awr的情况. 我们可以看到,该系统的load profile信息其实并不高,每秒才21个tra ...
- Swift 3中新的访问控制关键字fileprivate和open
在Swift 3中除去原有的3个访问控制关键字private,public,internal,又添加了2个关键字fileprivate和open 它们可以看成是对private和public的进一步细 ...