算法训练营第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;
    }
};
相关推荐
希望我万事顺遂5 小时前
力扣hot100-滑动窗口最大值
数据结构·算法·leetcode
꧁细听勿语情꧂5 小时前
二叉树余题,排序、直接插入排序、希尔排序
c语言·开发语言·数据结构·算法
是main不是漫5 小时前
【双指针|LeetCode1089 复写零】巧用逆向思路,破解原地覆盖陷阱
数据结构·算法·leetcode
专注仿真15 小时前
知识问答模型用到的算法集详解及代码实例
python·算法·代码·大模型训练·控制算法
罗西的思考16 小时前
【轻量化复刻大模型 / KIMI K3】AttnRes 模块交互式演示
人工智能·算法·机器学习
好悬给我拽开线20 小时前
ZILOT 论文解析
算法
饮水桶_T21 小时前
JAVA IO流笔记
开发语言·数据结构·算法
星夜离尘561 天前
MATLAB 离散数据分箱:半开区间、末端点与计数、加权和、平均
算法·matlab
Pniubi1 天前
力扣76.最小覆盖子串
算法·leetcode·职场和发展
鸿蒙开发爱好者1 天前
下班后做的小工具和小游戏,我把它们整理成了 WyanHub
网络·人工智能·职场和发展·个人开发