当前位置:网站首页>Force deduction 85 question maximum rectangle
Force deduction 85 question maximum rectangle
2022-06-29 09:58:00 【Yingtai night snow】
Power button 85 Question maximum rectangle
Title Description
Given a contains only 0 and 1 、 The size is rows x cols Two dimensional binary matrix , Find out that only 1 The largest rectangle of , And return its area .
I/o sample

Input :matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
Output :6
explain : The largest rectangle is shown above .
Input :matrix = []
Output :0
Input :matrix = [["0"]]
Output :0
Input :matrix = [["1"]]
Output :1
Solution one uses violent solution
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;
// Go through every line
for(int row=0;row<rlength;row++)
{
for(int col=0;col<clength;col++)
{
if(matrix[row][col]=='1')
{
// The current value saves the current row 1 Value
if(col==0)
{
width[row][col]=1;
}
else
{
width[row][col]=width[row][col-1]+1;
}
}
else{
width[row][col]=0;
}
// Record the minimum number of all rows
// Take the minimum value of the current column value as the width
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;
}
solution 2, Solve with stack
/ The idea of finding the maximum area in combination with the previous question
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];
}
// Create an array and save the sentry
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;
// Build stack , Put the sentry in the stack first
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;
}
边栏推荐
- Slider validation code
- How to traverse objects in the vector container
- C语言中通过sprintf()函数构造sql语句
- 数据仓库:金融/银行业的分层架构篇
- UE4 material UV texture does not stretch with model scale
- Cisco ASA、FTD和HyperFlex HX的漏洞分析复现
- Fabrication d'une calculatrice d'addition simple basée sur pyqt5 et Qt Designer
- linux下centos7中mysql5.7安装教程
- 内网穿透工具frp使用入门
- Leetcode skimming -- teponacci sequence
猜你喜欢

Install and configure redis in the Linux environment, and set the boot auto start

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

Zabbix4.4 configure the indicators of the monitoring server and solve the garbled graphics pages
![[Huawei certification] the most complete and selected question bank in hcia-datacom history (with answer analysis)](/img/d4/f5ea847573433f7ca7bd429f57e40a.png)
[Huawei certification] the most complete and selected question bank in hcia-datacom history (with answer analysis)

Hystrix熔断器:服务熔断与服务降级

力扣94二叉树的中序遍历

1424. 对角线遍历 II

Deep Learning-based Automated Delineation of Head and Neck Malignant Lesions from PET Images

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

A 3D Dual Path U-Net of Cancer Segmentation Based on MRI
随机推荐
聊聊你理解的线程与并发
2020-09-17 gateway业务流程 两个任务:referer认证和非商品模板化
leetcode MYSQL数据库题目181
你必须知道的23个最有用的Elasticseaerch检索技巧
Leetcode skimming -- teponacci sequence
力扣94二叉树的中序遍历
Zabbix4.4 configure the indicators of the monitoring server and solve the garbled graphics pages
How to set Google Chrome as the default browser
linux下centos7中mysql5.7安装教程
Mysql配置主从数据库
Visual assist plug-in settings for UE4 vs
阿里云服务器安装配置redis,无法远程访问
2020-09-21 referer字符串切分 boost gateway代码组织层次
Chang'an chain go language smart contract writing and compilation
gSoap例子——calc
Kicad learning notes - shortcut keys
【技术开发】酒精测试仪解决方案开发设计
Recyclerview refreshes blinks and crashes when deleting items
Closed training (25) basic web security
基于stm32标准库独立按键的多按键状态机的实现