当前位置:网站首页>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;
}
};
边栏推荐
- [WesternCTF2018]shrine
- Read the recent trends of okaleido tiger and tap the value and potential behind it
- 规划数学期末考试模拟二
- 承办首届算力大会,济南胜在何处?
- 【流放之路-第五章】
- 【搜索】—— 迭代加深/双向DFS/IDA*
- [hcip] OSPF experiment under mGRE environment, including multi process bidirectional republication and OSPF special area
- Comprehensive upgrade, all you can imagine is here -- JD API interface
- [hcip] MPLS Foundation
- 【GoLang】同步锁 Mutex
猜你喜欢

What are source code, inverse code and complement code

Moonbeam上的多链用例解析——Derek在Polkadot Decoded 2022的演讲文字回顾

560 and K

Six simple techniques to improve the value of penetration testing and save tens of thousands of yuan

Understand various paths
![关于df[‘某一列名’][序号]](/img/e2/179fb4eda695726e87bb483f65e04e.png)
关于df[‘某一列名’][序号]
![[hcip] experiment of republishing and routing strategy](/img/26/d62d3083796757d33c0a513f842176.png)
[hcip] experiment of republishing and routing strategy

九天后我们一起,聚焦音视频、探秘技术新发展

mysql的执行顺序

Read the recent trends of okaleido tiger and tap the value and potential behind it
随机推荐
Six noteworthy cloud security trends in 2022
[understanding of opportunity-54]: plain book-1-the origin of things [original chapter 1]: the road is simple.
Google Cloud Spanner的实践经验
Read the recent trends of okaleido tiger and tap the value and potential behind it
How to choose professional, safe and high-performance remote control software
[search] - iteration deepening / bidirectional dfs/ida*
[hcip] experiment of republishing and routing strategy
It is found that the data of decimal type in the database can be obtained through resultset.getdouble, but this attribute cannot be obtained through GetObject.
Practical experience of Google cloud spanner
Tomorrow infinite plan, 2022 conceptual planning scheme for a company's yuanuniverse product launch
ELS square movement
易观分析:以用户为中心,提升手机银行用户体验,助力用户价值增长
Use of packet capturing tool Charles
Super technology network security risk assessment service, comprehensively understand the security risks faced by the network system
Ruiji takeout project actual battle day01
Data security is a competitive advantage. How can companies give priority to information security and compliance
For a safer experience, Microsoft announced the first PC with a secure Pluto chip
规划数学期末模拟考试一
[7.21-26] code source - [sports festival] [Dan fishing war] [maximum weight division]
Where will Jinan win in hosting the first computing power conference?