前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >LeetCode 0084 - Largest Rectangle in Histogram

LeetCode 0084 - Largest Rectangle in Histogram

作者头像
Reck Zhang
发布于 2021-08-11 02:49:46
发布于 2021-08-11 02:49:46
22200
代码可运行
举报
文章被收录于专栏:Reck ZhangReck Zhang
运行总次数:0
代码可运行

Largest Rectangle in Histogram

Desicription

Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.

Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].

The largest rectangle is shown in the shaded area, which has area = 10 unit.

For example,

Given heights = [2,1,5,6,2,3],

return 10.

Solution

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        stack<pair<int, int>> s;
        int res = 0;
        for (int i = 0; i < heights.size(); i++) {
            if (s.empty() || s.top().first < heights[i])
                s.push(make_pair(heights[i], 1));
            else {
                int pre_weight = 0;
                while (s.size() && s.top().first >= heights[i]) {
                    pair<int, int> cur = s.top();
                    s.pop();
                    res = max(res, cur.first * cur.second);
                    if (s.size() && s.top().first >= heights[i])
                        s.top().second += cur.second;
                    pre_weight = cur.second;
                }
                s.push(make_pair(heights[i], 1 + pre_weight));
            }
        }
        while (s.size()) {
            pair<int, int> cur = s.top();
            s.pop();
            if (s.size())
                s.top().second += cur.second;
            res = max(res, cur.first * cur.second);
        }
        return res;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-11-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
【LEETCODE】模拟面试-84-Largest Rectangle in Histogram
题目: https://leetcode.com/problems/largest-rectangle-in-histogram/ Given n non-negative integers rep
杨熹
2018/04/03
6860
【LEETCODE】模拟面试-84-Largest Rectangle in Histogram
Leetcode-Hard 84. Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
致Great
2019/03/11
3500
Leetcode-Hard 84. Largest Rectangle in Histogram
Leetcode 84. Largest Rectangle in Histogram
版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://blog.csdn.net/Quincuntial/article/details/81190208
Tyan
2019/05/26
4240
LeetCode 84. Largest Rectangle in Histogram
单调栈是递增的,每个长方形入栈时,都和栈顶的长方形高度对比,如果大于,则入栈。如果小于则按照高度合并长方形,直到比它高度小的元素,然后再进栈。
ShenduCC
2019/11/16
5420
Leetcode 84 Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is
triplebee
2018/01/12
4810
Leetcode 84 Largest Rectangle in Histogram
56. 合并区间(思维)
题目链接:https://leetcode-cn.com/problems/merge-intervals/
Ch_Zaqdt
2020/02/17
3160
【leetcode刷题】T23-最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
木又AI帮
2019/07/17
4560
leetcode: 84. Largest Rectangle in Histogram
Problem # Given n non-negative integers representing the histogram's bar height where the width of e
JNingWei
2018/09/27
5780
leetcode: 84. Largest Rectangle in Histogram
hdu---1506(Largest Rectangle in a Histogram/dp最大子矩阵)
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11541    Accepted Submission(s): 3174 Problem Description A histogram is a polygon composed of a sequence of recta
Gxjun
2018/03/26
5910
hdu---1506(Largest Rectangle in a Histogram/dp最大子矩阵)
【leetcode刷题】T22-柱状图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
木又AI帮
2019/07/17
4200
【leetcode刷题】T22-柱状图中最大的矩形
单调栈结构 && 84. Largest Rectangle in Histogram&&最大子矩阵的大小
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
大学里的混子
2019/02/24
5480
[Leetcode][python]Largest Rectangle in Histogram
给定一个柱状图,求它能包含的最大的矩形的面积。如下图中阴影部分就是要求的矩形。
蛮三刀酱
2019/03/26
5940
[Leetcode][python]Largest Rectangle in Histogram
LeetCode <Stack>84&85. Maximal Rectangle&Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
大学里的混子
2018/11/20
4370
程序员进阶之算法练习(十七)
前言 正文6道题目来自leetcode––为求职为生的编程网站,目的是工作闲暇之时锤炼代码功底。 如何从这篇文章受益? 先看题目大意,对照Example的样例理解题目意思; 尝试解答,得到自己的答案,并分析时间和空间复杂度; 思考完毕,看题目解析,对比分析自己解法的异同和优劣。 题目大都是LeetCode的hard难度。 正文 41.First Missing Positive 题目链接 题目大意: 给出一个数组,找到数组中没有的最小正整数。 要求:O(N)的时间和O(1)的空间复杂度;
落影
2018/04/27
9340
程序员进阶之算法练习(十七)
HDU 1506 Largest Rectangle in a Histogram(单调栈)
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 20760    Accepted Submission(s): 6325 Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l
attack
2018/05/30
6980
​LeetCode刷题实战84: 柱状图中最大的矩形
https://leetcode-cn.com/problems/largest-rectangle-in-histogram/
程序员小猿
2021/01/19
4320
​LeetCode刷题实战84:  柱状图中最大的矩形
算法细节系列(12):破除想当然
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/70820874
用户1147447
2019/05/26
3220
Leetcode 85 Maximal Rectangle 推荐!
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 6. 求最大子矩阵。 和84如出一辙,http://blog.csdn.net/accepthj
triplebee
2018/01/12
6350
Leetcode 150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/",
triplebee
2018/01/12
6830
算法刷题笔记03:Stack、Queue
给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。
Hsinyan
2022/08/30
2890
算法刷题笔记03:Stack、Queue
相关推荐
【LEETCODE】模拟面试-84-Largest Rectangle in Histogram
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验