当前位置:网站首页>剑指offer专项突击版第13天
剑指offer专项突击版第13天
2022-07-29 00:51:00 【hys__handsome】
剑指 Offer II 039. 直方图最大矩形面积
容易发现每个位置在扩展时,遇到比当前位置矮的位置就无法扩展了,所以通过单调栈扩展每个位置左右边界最大的范围。
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; //左边界
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; //右边界
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;
}
};
剑指 Offer II 040. 矩阵中最大的矩形
动态规划,记录二维矩阵每个位置左边最大连续1的长度,然后遍历每个二维矩阵位置,并向上扩展,如果上面的连续长度比当前位置短那么就取短的,每次扩展取一次最大面积。
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;
}
};
单调栈优化动态规划
把这个连续的1序列当成柱形,整个二维矩阵翻转一下就变成直方图最大矩形面积了,然后思路是一样的,只是这里多了一维需要多算m-1列
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;
}
};
边栏推荐
- Error installing mysqlclient module on MAC system
- 活动速递| Apache Doris 性能优化实战系列直播课程初公开,诚邀您来参加!
- 第二轮Okaleido Tiger热卖的背后,是背后生态机构战略支持
- JS judge whether array / object array 1 contains array / object array 2
- 2022年最火的十大测试工具,你掌握了几个
- 云原生应用综合练习上
- Timer of BOM series
- mysql 创建索引的三种方式
- 过去10年的10起重大网络安全事件
- Understand all the basic grammar of ES6 in one article
猜你喜欢
![[SQL's 18 dragon subduing palms] 01 - Kang long regrets: introductory 10 questions](/img/db/d0255d7036f7003d380c8888fed88b.png)
[SQL's 18 dragon subduing palms] 01 - Kang long regrets: introductory 10 questions

SQL question brushing: find the last of all employees who have been assigned departments_ Name and first_ Name and Dept_ no
![关于df[‘某一列名’][序号]](/img/e2/179fb4eda695726e87bb483f65e04e.png)
关于df[‘某一列名’][序号]

Teach you a text to solve the problem of JS digital accuracy loss

Read the recent trends of okaleido tiger and tap the value and potential behind it

【HCIP】重发布及路由策略的实验

PLATO上线LAAS协议Elephant Swap,用户可借此获得溢价收益

J9数字论:什么因素决定NFT的价值?

Openpyxl library fill color

第二轮Okaleido Tiger热卖的背后,是背后生态机构战略支持
随机推荐
BOM系列之window对象
How many of the top ten test tools in 2022 do you master
Three ways of creating indexes in MySQL
SQL question brushing: find the employee number EMP with more than 15 salary records_ No and its corresponding recording times t
瑞吉外卖项目实战Day01
[SQL's 18 dragon subduing palms] 01 - Kang long regrets: introductory 10 questions
代码生成器
SQL injection of DVWA
redis安装,集群部署与常见调优
T-sne dimensionality reduction
Django uses pymysql module to connect mysql database
The solution to keep the height of the container unchanged during the scaling process of the uniapp movable view table
After understanding the composition of the URL of the website, we use the URL module, querystring module and mime module to improve the static website
5G 商用第三年:无人驾驶的“上山”与“下海”
numpy.where() 用法和np.argsort()的用法
Redis is installed on Linux
Docuware mobile labor solution can help you build a new productivity model: anytime, anywhere, any device
新1688 API 接入说明
Basic label in body
We summarized the three recommendations for the use of Nacos and first published the Nacos 3.0 plan for the 4th anniversary of the open source of Nacos