算法训练营第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;
    }
};
相关推荐
cynicme1 天前
力扣3318——计算子数组的 x-sum I(偷懒版)
java·算法·leetcode
im_AMBER1 天前
算法笔记 09
c语言·数据结构·c++·笔记·学习·算法·排序算法
凯芸呢1 天前
Java中的数组(续)
java·开发语言·数据结构·算法·青少年编程·排序算法·idea
寂静山林1 天前
UVa 1030 Image Is Everything
算法
AI柠檬1 天前
几种排序算法的实现和性能比较
数据结构·算法·c#·排序算法
weixin_429630261 天前
第6章 支持向量机
算法·机器学习·支持向量机
SweetCode1 天前
C++ 实现大数加法
开发语言·c++·算法
王哈哈^_^1 天前
【数据集】【YOLO】【目标检测】共享单车数据集,共享单车识别数据集 3596 张,YOLO自行车识别算法实战训推教程。
人工智能·算法·yolo·目标检测·计算机视觉·视觉检测·毕业设计
CodeWizard~1 天前
AtCoder Beginner Contest 430赛后补题
c++·算法·图论