算法训练营第63天|LeetCode 84.柱状图中最大的矩形

完结!撒花!

LeetCode 84.柱状图中最大的矩形

题目链接:

LeetCode 84.柱状图中最大的矩形

代码:

cpp 复制代码
class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        heights.insert(heights.begin(),0);
        heights.push_back(0);
        int size = heights.size();
        stack<int>st;
        int result = 0;
        st.push(0);
        for(int i=1;i<size;i++){
            if(heights[i]>=heights[st.top()]) st.push(i);
            else{
                while(!st.empty() && heights[i]<heights[st.top()]){
                    int mid = st.top();
                    st.pop();
                    if(!st.empty()){
                        int right = i;
                        int left = st.top();
                        int w = right - left - 1;
                        result = max(result, w*heights[mid]);
                    }
                }
                st.push(i);
            }
        }
        return result;
    }
};
相关推荐
Angelina_Jolie16 分钟前
一文搞懂 SCI、SSCI、CSSCI、C 刊、核心期刊:定义、作用、层级对比及投稿选择
考研·职场和发展·创业创新
m0_7487080517 分钟前
C++中的观察者模式实战
开发语言·c++·算法
然哥依旧17 分钟前
【轴承故障诊断】基于融合鱼鹰和柯西变异的麻雀优化算法OCSSA-VMD-CNN-BILSTM轴承诊断研究【西储大学数据】(Matlab代码实现)
算法·支持向量机·matlab·cnn
qq_5375626729 分钟前
跨语言调用C++接口
开发语言·c++·算法
Tingjct41 分钟前
【初阶数据结构-二叉树】
c语言·开发语言·数据结构·算法
C雨后彩虹42 分钟前
计算疫情扩散时间
java·数据结构·算法·华为·面试
yyy(十一月限定版)1 小时前
寒假集训4——二分排序
算法
星火开发设计1 小时前
类型别名 typedef:让复杂类型更简洁
开发语言·c++·学习·算法·函数·知识
醉颜凉2 小时前
【LeetCode】打家劫舍III
c语言·算法·leetcode·树 深度优先搜索·动态规划 二叉树
达文汐2 小时前
【困难】力扣算法题解析LeetCode332:重新安排行程
java·数据结构·经验分享·算法·leetcode·力扣