当前位置:网站首页>剑指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;
}
};
边栏推荐
- Openpyxl border
- [MySQL] historical cumulative de duplication of multiple indicators
- A ten thousand word blog post takes you into the pit. Reptiles are a dead end [ten thousand word pictures]
- C语言犄角旮旯的知识之形参、实参、main函数参数、数组或指针做函数参数等
- Window object of BOM series
- Pinduoduo can use many API interfaces
- internship:用于类型判断的工具类编写
- els 新的方块落下
- 过去10年的10起重大网络安全事件
- Docker-compose安装mysql
猜你喜欢

Introduction to Elmo, Bert and GPT

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

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

Recommended Spanish translation of Beijing passport

redis安装,集群部署与常见调优

Test / development programmers rely on technology to survive the midlife crisis? Improve your own value
![[hcip] MPLS Foundation](/img/91/a2aebf333fb3e3fedf0fc393e175a9.png)
[hcip] MPLS Foundation

SiC功率半导体产业高峰论坛成功举办
![[leetcode sliding window problem]](/img/84/566d3805e52c358603694cdec69a13.png)
[leetcode sliding window problem]

T-sne降维
随机推荐
Alphafold revealed the universe of protein structure - from nearly 1million structures to more than 200million structures
Comprehensive upgrade, all you can imagine is here -- JD API interface
Log4j dynamic loading configuration file
Tomorrow infinite plan, 2022 conceptual planning scheme for a company's yuanuniverse product launch
Moonbeam上的多链用例解析——Derek在Polkadot Decoded 2022的演讲文字回顾
Plato launched the LAAS protocol elephant swap, which allows users to earn premium income
[search] - iteration deepening / bidirectional dfs/ida*
活动速递| Apache Doris 性能优化实战系列直播课程初公开,诚邀您来参加!
数据库的decimal类型的数据,发现可以通过resultSet.getDouble去拿到这个数据,但是通过getObject却拿不到这个属性。
SiC Power Semiconductor Industry Summit Forum successfully held
DocuWare 移动劳动力解决方案可帮助您构建新的生产力模式:随时随地、任何设备
嵌入式分享合集23
Django reports an error using pymsql module django.db.utils.operationalerror
numpy.where() 用法和np.argsort()的用法
PCL point cloud intensity image
els 方块移动
SQL question brushing: find the employee number EMP with more than 15 salary records_ No and its corresponding recording times t
BOM系列之定时器
[hcip] two mGRE networks are interconnected through OSPF (ENSP)
【搜索】—— DFS之剪枝与优化