算法训练营第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;
    }
};
相关推荐
WolfGang0073219 分钟前
代码随想录算法训练营 Day17 | 二叉树 part07
算法
温九味闻醉10 分钟前
关于腾讯广告算法大赛2025项目分析1 - dataset.py
人工智能·算法·机器学习
炽烈小老头17 分钟前
【 每天学习一点算法 2026/03/23】数组中的第K个最大元素
学习·算法·排序算法
老鱼说AI26 分钟前
大规模并发处理器程序设计(PMPP)讲解(CUDA架构):第四期:计算架构与调度
c语言·深度学习·算法·架构·cuda
月落归舟29 分钟前
帮你从算法的角度来认识数组------( 二 )
数据结构·算法·数组
阿贵---1 小时前
C++中的RAII技术深入
开发语言·c++·算法
NAGNIP1 小时前
面试官:深度学习中经典的优化算法都有哪些?
算法
PiKaMouse.1 小时前
navigation2-humble从零带读笔记第一篇:nav2_core
c++·算法·机器人
木井巳1 小时前
【递归算法】子集
java·算法·leetcode·决策树·深度优先
lightqjx2 小时前
【算法】二分算法
c++·算法·leetcode·二分算法·二分模板