当前位置:网站首页>Sword finger offer special assault edition day 13
Sword finger offer special assault edition day 13
2022-07-29 01:51:00 【hys__ handsome】
The finger of the sword Offer II 039. Maximum rectangular area of histogram
It is easy to find that each location is expanding , If you encounter a position that is shorter than the current position, you cannot expand , So expand the maximum range of the left and right boundaries of each position by monotonic stack .
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int n = heights.size();
int l[100010], r[100010] = {
0}, top = -1, stk[100010];
for(int i = 0; i < n; i++) {
while(top >= 0 && heights[i] <= heights[stk[top]]) {
top--;
}
if(top >= 0) l[i] = stk[top];
else l[i] = -1; // Left boundary
stk[++top] = i;
}
top = -1;
for(int i = n-1; i >= 0; i--) {
while(top >= 0 && heights[i] <= heights[stk[top]]) top--;
if(top >= 0) r[i] = stk[top];
else r[i] = n; // Right border
stk[++top] = i;
}
int res = -0x3f3f3f3f;
for(int i = 0; i < n; i++) res = max(res,heights[i]*(r[i]-l[i]-1));
return res;
}
};
The finger of the sword Offer II 040. The largest rectangle in the matrix
Dynamic programming , Record the maximum continuity on the left of each position of the two-dimensional matrix 1 The length of , Then traverse each two-dimensional matrix position , And expand upward , If the continuous length above is shorter than the current position, then take the shorter , Take the maximum area for each expansion .
class Solution {
public:
int maximalRectangle(vector<string>& matrix) {
int n = matrix.size();
if(n == 0) return 0;
int m = matrix[0].size();
if(m == 0) return 0;
vector<vector<int>> left(n,vector<int>(m,0));
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(matrix[i][j] == '1') {
if(j > 0) left[i][j] = left[i][j-1] + 1;
else left[i][j] = 1;
}
int res = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(matrix[i][j] == '0') continue;
int area = left[i][j];
int width = left[i][j];
for(int k = i-1; k >= 0; k--) {
width = min(width,left[k][j]);
area = max(area,width*(i-k+1));
}
res = max(res,area);
}
}
return res;
}
};
Monotonic stack optimization dynamic programming
Put this continuous 1 Treat the sequence as a column , The entire two-dimensional matrix turns into the largest rectangular area of the histogram , Then the idea is the same , It's just that there's one more dimension here, so we need to calculate more m-1 Column
class Solution {
public:
int maximalRectangle(vector<string>& matrix) {
int n = matrix.size();
if(n == 0) return 0;
int m = matrix[0].size();
if(m == 0) return 0;
vector<vector<int>> left(n,vector<int>(m,0));
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(matrix[i][j] == '1') {
if(j > 0) left[i][j] = left[i][j-1] + 1;
else left[i][j] = 1;
}
vector<int> up(n), down(n);
int res = 0;
for(int j = 0; j < m; j++) {
stack<int> stk;
up = vector<int>(n),down = vector<int>(n);
for(int i = 0; i < n; i++) {
while(stk.size() && left[i][j] <= left[stk.top()][j]){
stk.pop();
}
if(stk.size()) up[i] = stk.top();
else up[i] = -1;
stk.push(i);
}
stk = stack<int>();
for(int i = n-1; i >= 0; i--) {
while(stk.size() && left[i][j] <= left[stk.top()][j]){
stk.pop();
}
if(stk.size()) down[i] = stk.top();
else down[i] = n;
stk.push(i);
}
for(int i = 0; i < n; i++) {
res = max(res,left[i][j] * (down[i] - up[i] - 1));
}
}
return res;
}
};
边栏推荐
- [7.27] code source - [deletion], [bracket sequence], [number replacement], [game], [painting]
- 【Web技术】1395- Esbuild Bundler HMR
- 5G 商用第三年:无人驾驶的“上山”与“下海”
- 动态内存与智能指针
- StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
- Practical experience of Google cloud spanner
- Cloud native application comprehensive exercise
- What is the ISO assessment? How to do the waiting insurance scheme
- [web technology] 1395 esbuild bundler HMR
- 【流放之路-第八章】
猜你喜欢

【7.21-26】代码源 - 【体育节】【丹钓战】【最大权值划分】

Cloud native application comprehensive exercise

【Unity项目实践】合成大西瓜

mysql的执行顺序

JVM learning minutes

5g commercial third year: driverless "going up the mountain" and "going to the sea"

StoneDB 邀请您参与开源社区月会!

Super scientific and technological data leakage prevention system, control illegal Internet behaviors, and ensure enterprise information security

BOM系列之window对象

Reinforcement learning (I): Q-learning, with source code interpretation
随机推荐
【Golang】- runtime.Goexit()
SiC Power Semiconductor Industry Summit Forum successfully held
What are source code, inverse code and complement code
MySQL execution order
Practical experience of Google cloud spanner
Tda75610-i2c-determination of I2C address of analog power amplifier
DSP震动座椅
【HCIP】MGRE环境下OSPF实验,含多进程双向重发布及OSPF特殊区域
【7.21-26】代码源 - 【平方计数】【字典序最小】【“Z”型矩阵】
Leetcode 112: path sum
TDA75610-I2C-模拟功放I2C地址的确定
golang启动报错【已解决】
[WesternCTF2018]shrine
正则过滤数据学习笔记(①)
[WesternCTF2018]shrine
【HCIP】重发布及路由策略的实验
How many of the top ten test tools in 2022 do you master
Sigma-DSP-OUTPUT
【流放之路-第五章】
OpenGL development with QT (II) drawing cube