C++ | Leetcode C++题解之第84题柱状图中最大的矩形

题目:

题解:

cpp 复制代码
class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        int n = heights.size();
        vector<int> left(n), right(n, n);
        
        stack<int> mono_stack;
        for (int i = 0; i < n; ++i) {
            while (!mono_stack.empty() && heights[mono_stack.top()] >= heights[i]) {
                right[mono_stack.top()] = i;
                mono_stack.pop();
            }
            left[i] = (mono_stack.empty() ? -1 : mono_stack.top());
            mono_stack.push(i);
        }
        
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            ans = max(ans, (right[i] - left[i] - 1) * heights[i]);
        }
        return ans;
    }
};
相关推荐
初願致夕霞3 小时前
C++11:类型推导与完美转发复盘笔记
c++
小猪写代码3 小时前
C++中什么是类,什么是对象
c++
mmmmath_35 小时前
LeetCode.541.反转字符串II
数据结构·算法·leetcode
Navigator_Z5 小时前
LeetCode //MySQL - 1251. Average Selling Price
c语言·算法·leetcode
钓鱼的肝7 小时前
梳理(1-5)
c++·经验分享·笔记·算法·青少年编程
参.商.7 小时前
【Day 53】76. 最小覆盖子串
leetcode·golang
jimy18 小时前
readelf 查看“派生类”的vtable符号
开发语言·c++
蒸蒸yyyyzwd8 小时前
cpp 选手秋招学习笔记 day37 面经学习
c++·八股
YYYing.8 小时前
【C++进阶系列 (七)】C++ RTTI 深度剖析:从 typeid 到 dynamic_cast 的底层之旅
开发语言·c++·c/c++·rtti
虚无的纽扣9 小时前
【力扣刷题】第二天:无重复字符的最长字串、移动零问题
算法·leetcode·排序算法