当前位置:网站首页>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;
}
边栏推荐
- 2020-09-29 非商品模板化代码层次 rapidjson库
- 语言特性
- Deep Learning-based Automated Delineation of Head and Neck Malignant Lesions from PET Images
- 2020-09-17 gateway业务流程 两个任务:referer认证和非商品模板化
- Data governance: Metadata Management (Part 2)
- A comparison of methods for fully automatic segmentation of tumors and involved nodes in PET/CT of h
- 数据仓库:金融/银行业的分层架构篇
- 微信小程序实现数据侦听器watch,包含销毁watch和子属性的watch
- Visual assist plug-in settings for UE4 vs
- Student addition / deletion gaih
猜你喜欢

Making of simple addition calculator based on pyqt5 and QT Designer

Lc236. nearest common ancestor of binary tree
![[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)

Visual assist plug-in settings for UE4 vs

装饰器模式的应用,包装ServletRequest,增加addParameter方法

FreeRTOS(九)——队列

ORA-01950 对表空间无权限

A comparison of methods for fully automatic segmentation of tumors and involved nodes in PET/CT of h

Automatic 3D Detection and Segmentation of Head and Neck Cancer from MRI Data.

基于keil5自动配置stm32f103标准库的官网freertos移植
随机推荐
A 2.5D Cancer Segmentation for MRI Images Based on U-Net
Automatic 3D Detection and Segmentation of Head and Neck Cancer from MRI Data.
Visual assist plug-in settings for UE4 vs
Fabrication d'une calculatrice d'addition simple basée sur pyqt5 et Qt Designer
watch监听和computed计算属性的使用和区别
指针函数和函数指针
Mysql5.7 installation tutorial in centos7 under Linux
Construction and use of Changan chain go language smart contract environment
Cisco ASA、FTD和HyperFlex HX的漏洞分析复现
c#判断数组是否包含另一个数组的任何项
基於PyQt5和Qt Designer的簡易加法計算器的制作
C语言实现一种创建易管理易维护线程的方法
367. 有效的完全平方数-二分法
Automatic Multi-Organ SegmVentation on Abdominal CT With Dense V-Networks
How to set Google Chrome as the default browser
linux下centos7中mysql5.7安装教程
linux环境下安装配置redis,并设置开机自启动
Closed training (25) basic web security
General part: cognition, design and best practice of prototype design
共用体Union