当前位置:网站首页>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;
}
边栏推荐
- [Huawei certification] the most complete and selected question bank in hcia-datacom history (with answer analysis)
- 力扣85题最大矩形
- 滑块验证代码
- watch监听和computed计算属性的使用和区别
- How to set Google Chrome as the default browser
- 367. 有效的完全平方数-二分法
- 阿里云服务器安装配置redis,无法远程访问
- 安装Anaconda后启动JupyterLab需要输入密码
- Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit
- 力扣94二叉树的中序遍历
猜你喜欢

A 3D Dual Path U-Net of Cancer Segmentation Based on MRI

Data visualization: the four quadrants of data visualization teach you to correctly apply icons

Chang'an chain go language smart contract writing and compilation

Fabrication d'une calculatrice d'addition simple basée sur pyqt5 et Qt Designer

Data governance: data standard management (Part III)

完美二叉树、完全二叉树、完满二叉树

Student addition / deletion gaih

你知道BFD是什么吗?一文详解BFD协议原理及使用场景

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

UE4 blueprint modify get a copy in array to reference
随机推荐
监控数据源连接池使用情况
A 2.5D Cancer Segmentation for MRI Images Based on U-Net
2020-09-23左右值 右值引用 std::move()
【华为认证】HCIA-DATACOM史上最全精选题库(附答案解析)
Application of decorator mode, packaging ServletRequest and adding addparameter method
微信小程序重写Page函数,实现全局日志记录
JS obtain mobile phone model and system version
IDEA自动补全
2020-09-18 referer认证 url转义
Custom MVC framework implementation
監控數據源連接池使用情况
gSoap例子——calc
Implementation of multi key state machine based on STM32 standard library
mysql修改自动递增初始值
Causes and solutions of error reporting by using startactivity() method outside the activity
动态规划总结
es报错NoNodeAvailableException[None of the configured nodes are available:[.127.0.0.1}{127.0.0.1:9300]
ImageView picture fill problem
Fabrication d'une calculatrice d'addition simple basée sur pyqt5 et Qt Designer
c#判断数组是否包含另一个数组的任何项