当前位置:网站首页>力扣85题最大矩形
力扣85题最大矩形
2022-06-29 09:11:00 【瀛台夜雪】
力扣85题最大矩形
题目描述
给定一个仅包含 0 和 1 、大小为 rows x cols 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。
输入输出样例

输入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
输出:6
解释:最大矩形如上图所示。
输入:matrix = []
输出:0
输入:matrix = [["0"]]
输出:0
输入:matrix = [["1"]]
输出:1
解法一使用暴力解法
int maximalRectangle2(vector<vector<char>>&matrix)
{
int rlength=matrix.size();
int clength=matrix[0].size();
if(rlength==0)
{
return 0;
}
vector<vector<int>>width(rlength,vector<int>(clength));
int maxArea=0;
//遍历每一行
for(int row=0;row<rlength;row++)
{
for(int col=0;col<clength;col++)
{
if(matrix[row][col]=='1')
{
//当前值保存当前连续一行1的值
if(col==0)
{
width[row][col]=1;
}
else
{
width[row][col]=width[row][col-1]+1;
}
}
else{
width[row][col]=0;
}
//记录所有行中最小的数
//以当前列值的最小值作为宽
int minWidth=width[row][col];
for(int upRow=row;upRow>=0;upRow--)
{
int height=row-upRow+1;
minWidth=min(minWidth,width[upRow][col]);
maxArea=max(maxArea,height*minWidth);
}
}
}
return maxArea;
}
解法2,利用堆栈求解
/结合上一题求面积最大值的思路
int maximalRectangle(vector<vector<char>>&matrix)
{
int rlength=matrix.size();
int clength=matrix[0].size();
vector<vector<int>>res(rlength,vector<int>(clength));
for(int i=0;i<rlength;i++)
{
for(int j=0;j<clength;j++)
{
if(matrix[i][j]=='1')
{
res[i][j]=1;
}
else{
res[i][j]=0;
}
}
}
for(int i=0;i<rlength-1;i++)
{
for(int j=0;j<clength;j++)
{
if(res[i+1][j])
{
res[i+1][j]=res[i+1][j]+res[i][j];
}
}
}
int maxArea=0;
for(int i=0;i<rlength;i++)
{
int temp=largestRectangleArea(res[i]);
maxArea=max(temp,maxArea);
}
return maxArea;
}
int largestRectangleArea(vector<int>&height)
{
int length=height.size();
if(length==0)
{
return 0;
}
else if(length==1)
{
return height[0];
}
//创建数组并保存哨兵
vector<int>tempList(length+2);
for(int i=0;i<length;i++)
{
tempList[i+1]=height[i];
}
tempList[0]=0;
tempList[length+1]=0;
int maxArea=0;
length+=2;
height=tempList;
//建立堆栈,将哨兵先入堆栈
stack<int>stk;
stk.push(0);
for(int i=1;i<length;i++)
{
while(height[stk.top()]>height[i])
{
int high=height[stk.top()];
stk.pop();
int width=i-stk.top()-1;
maxArea=max(maxArea,high*width);
}
stk.push(i);
}
return maxArea;
}
边栏推荐
- Could not open JDBC connection for transaction
- Mac mysql数据库基本操作
- User level threads and kernel level threads
- kdevelop新建工程
- 我想要股票开户优惠,怎么得到?还有,在线开户安全么?
- Visual assist plug-in settings for UE4 vs
- 监控数据源连接池使用情况
- UE4 blueprint modify get a copy in array to reference
- 你必须知道的23个最有用的Elasticseaerch检索技巧
- 1.4 regression of machine learning methods
猜你喜欢

阿里云服务器安装配置redis,无法远程访问

Do you know what BFD is? This article explains the principle and usage scenarios of BFD protocol in detail

基于PyQt5和Qt Designer的简易加法计算器的制作

KiCad学习笔记--快捷键

【华为认证】HCIA-DATACOM史上最全精选题库(附答案解析)

基于keil5自动配置stm32f103标准库的官网freertos移植

float 与 int 相乘产生的令人崩溃的“ 2.3 * 10 = 22 ”

Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit

CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION

linux环境下安装配置redis,并设置开机自启动
随机推荐
你知道BFD是什么吗?一文详解BFD协议原理及使用场景
动态规划总结
[noi Simulation Competition] add points for noi (heavy chain dissection, line segment tree)
cenos7下搭建LAMP环境
Wechat applet rewrites the page function to realize global logging
2020-09-18 referer认证 url转义
Data visualization: the significance of data visualization
Monitoring data source connection pool usage
IDEA自动补全
LC236. 二叉树的最近公共祖先
JS获取手机型号和系统版本
User level threads and kernel level threads
装饰器模式的应用,包装ServletRequest,增加addParameter方法
Simplicity studio does not recognize the new JLINK V9 solution
Easyexcl export 1million lines of EXECL report font error solution
367. effective complete square dichotomy
Chang'an chain go language smart contract writing and compilation
基于PyQt5和Qt Designer的简易加法计算器的制作
[technology development] development and design of alcohol tester solution
367. 有效的完全平方数-二分法