【一天一道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 ...
随机推荐
- 【DDD】--好文收藏
发现一批好文,完整系列,攒~~ 随笔分类 - DDD - 『圣杰』 DDD理论学习系列(1)-- 通用语言 笔记: 通用语言: a) 简单,便于理解.传播. b) 需要通用,能够准确的传达业务规则. ...
- C# ref与out
ref参数是引用,out参数为输出参数.我写一个控制台的程序来说明一下两者的特点和区别: class Program { 3 public static void RefMethod( ref int ...
- C++笔记003:从一个小程序开始
原创笔记,转载请注明出处! 点击[关注],关注也是一种美德~ 安装好VS2010后,从第一个小程序开始. 在学习C语言时,我首先输出了一个程序员非常熟悉的对这个世界的问候:hello world! ...
- POJ 放苹果问题(递归)
首先我们想象有一个函数count f(m,n)可以把m个苹果放到n个盘子中. 根据 n 和 m 的关系可以进一步分析: 特殊的m <=1|| n <= 1时只有一种方法: 当 m < ...
- UltraISO安装centos7系统
1. 使用最新版UltraISO将ISO镜像刻录到U盘一定要是最新版,试用版都可以,按下图操作: 2. U盘启动电脑进入安装界面正常情况下你应该会看到下面的这个界面: 选择第一项,然后按TAB键(在评 ...
- AC的故事大结局山寨版(下)(最大流)
福建工程学院第十二届ACM程序设计大赛真题 AC的故事大结局山寨版(下) TimeLimit:2000MS MemoryLimit:128MB 64-bit integer IO format:%l ...
- ZooKeeper之(一)ZooKeeper是什么
1.1 产生背景 当今是个分布式.集群.云计算等名词满天飞的时代.造成这种局面的一个重要因素就是,单一机器的处理能力已经不能满足我们的需求,不得不采用由多台机器组成的服务集群.服务集群对外提供服务的过 ...
- Swift3中dispatch_once废弃的解决办法
在Swift中如果想搞类的单例模式,那么在初始化的时候一般会使用just one time执行的方式,我们使用dispatch_once_t配合调用dispatch_once方法,一般的代码如下: s ...
- 用scheme最基本的元素定义排序函数
用到的元素有9个: define,if,null?,cons car,cdr,lambda,let,named let, 其实let 和 named let可以去掉.但那样会带来性能和可读性下降的问题 ...
- Python 一个奇特的引用设定
def f(x): print 'original' if x > 0: return f(x-1) return 0 g = f def f(x): print 'new' return x ...