算法训练营第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;
    }
};
相关推荐
MediaTea30 分钟前
AI 术语通俗词典:C4.5 算法
人工智能·算法
Navigator_Z41 分钟前
LeetCode //C - 1033. Moving Stones Until Consecutive
c语言·算法·leetcode
WBluuue43 分钟前
数据结构与算法:莫队(一):普通莫队与带修莫队
c++·算法
风筝在晴天搁浅1 小时前
n个六面的骰子,扔一次之后和为k的概率是多少?
算法
MATLAB代码顾问3 小时前
Python实现蜂群算法优化TSP问题
开发语言·python·算法
代码飞天3 小时前
机器学习算法和函数整理——助力快速查阅
人工智能·算法·机器学习
jiushiapwojdap3 小时前
LU分解法求解线性方程组Matlab实现
数据结构·其他·算法·matlab
笨笨饿3 小时前
69_如何给自己手搓一个串口
linux·c语言·网络·单片机·嵌入式硬件·算法·个人开发
纽扣6674 小时前
【算法进阶之路】链表进阶:删除、合并、回文与排序全解析
数据结构·算法·链表
消失的旧时光-19434 小时前
统一并发模型:线程、Reactor、协程本质是一件事(从线程到协程 · 第6篇·终章)
java·python·算法