【一天一道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 ...
随机推荐
- Socket.io应用之联网拖拽游戏
服务器端代码: const express=require('express'); const http=require('http'); const sio=require('socket.io') ...
- CTR预估算法
GBRT(Gradient Boost Regression Tree)渐进梯度回归树,XGBoost是GBRT的一个工程实现 LR(Logistics Regression )逻辑回归 Spark ...
- Python强大的可变参数传递机制
今天模拟定义map函数.写着写着就发现Python可变长度参数的机制真是灵活而强大. 假设有一个元组t,包含n个成员: t=(arg1,...,argn) 而一个函数f恰好能接受n个参数: f(arg ...
- 数学API Math.atan() 和Math.atan2() 三角函数复习
今天在学习贝塞尔曲线看到需要结合三角函数 以及两个不认识的Api :API Math.atan() 和Math.atan2() 先看下三角函数 正切函数图:(180为一个周期 即45=45+180) ...
- 剑指Offer——如何做好自我介绍(英文版)
剑指Offer--如何做好自我介绍(英文版) Good morning ladies and gentlemen, my name is Sun Huaqiang, my hometown loc ...
- Markdown对应Yelee主题语法
概述 这里说的是Yelee主题的语法和原生语法是有些区别的:更多的基础语法可以到Cmd Markdown上面去查看:但是我觉得都会各有不同吧 注意这里说的不是真正意义上的Markdown语法 标题 一 ...
- RxJava操作符(04-过滤操作)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51656494 本文出自:[openXu的博客] 目录: Debounce Distinct ...
- Spark技术内幕: Shuffle详解(一)
通过上面一系列文章,我们知道在集群启动时,在Standalone模式下,Worker会向Master注册,使得Master可以感知进而管理整个集群:Master通过借助ZK,可以简单的实现HA:而应用 ...
- nfc开发
很多Android设备已经支持NFC(近距离无线通讯技术)了.本文就以实例的方式,为大家介绍如何在Android系统中进行NFC开发. Android NFC开发环境 使用硬件:Google Nexu ...
- Maven 介绍、安装使用
简介 Maven是一个强大的构建工具,能够帮我们自动化构建过程,从清理.编译.测试到生成报告,再到打包和部署.只要使用Maven配置好项目,然后执行命令(如mvn clean inst ...