【一天一道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 ...
随机推荐
- Python内置方法的时间复杂度
转载自:http://www.orangecube.NET/Python-time-complexity 本页面涵盖了Python中若干方法的时间复杂度(或者叫"大欧"," ...
- Django笔记--视图
URLconf 在settings.py文件中通过ROOT_URLCONF指定根级url的配置 urlpatterns是一个url()实例的列表 一个url()对象包括: 正则表达式 视图函数 名称n ...
- Go 处理yaml类型的配置文件
先说一下,这里用到了很多关于反射类型的功能,可能刚开始看代码,如果对反射不熟悉的可能会不是非常清晰,但是同时也是为了更好的理解golang中的反射,同时如果后面想在代码中可以直接从我的git地址get ...
- 如果将Joomla网站搜索结果显示到一个“干净”页面
有时候大家会发现Joomla网站自带的或者第三方的搜索功能时,搜索结果会显示在首页,和首页其它的模块如图片橱窗等显示在一起,非常混乱. 在这里教大家一个不需要修改代码的小技巧来解决这个问题,使搜索结果 ...
- 用ECMAScript4 ( ActionScript3) 实现Unity的热更新 -- CustomYieldInstruction 自定义中断指令
ActionScript3脚本引擎为了方便热更新逻辑开发,提供的从脚本继承Unity类库功能在一些情况下可以提供开发的便利. 这次来建立一个示例,演示一下如何在脚本中自定义协程中断指令 Unity中的 ...
- 使用ffmpeg转码时遇到aac报错
今天尝试用ffmpeg转一个视频的格式,结果报出这个错误: The encoder 'aac' is experimental but experimental codecs are not enab ...
- 转:linux/unix命令行终端的光标及字符控制快捷键
from:http://linux.chinaunix.net/techdoc/system/2007/11/23/973027.shtml 在使用linux/unix的命令行终端时,有时候会碰到键盘 ...
- python笔记八(切片)
一.切片 首先我们要记得在Python中可以用于切片的对象有 列表.元组.字符串. 切片操作就是直接从列表.元组或字符串中,选择出我们想要的内容,这些操作非常简洁实用. >>> L ...
- Go 语言变量作用域
作用域为已声明标识符所表示的常量.类型.变量.函数或包在源代码中的作用范围. Go 语言中变量可以在三个地方声明: 函数内定义的变量称为局部变量 函数外定义的变量称为全局变量 函数定义中的变量称为形式 ...
- MongoDB 条件操作符
描述 条件操作符用于比较两个表达式并从mongoDB集合中获取数据. 在本章节中,我们将讨论如何在MongoDB中使用条件操作符. MongoDB中条件操作符有: (>) 大于 - $gt (& ...