[LeetCode]*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.

这里写图片描述

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 height = [2,1,5,6,2,3],
return 10.

思路

我们通过一个栈记录上升的柱子,如果如果下降的柱子,可以开始计算栈顶和之前柱子构建的矩形的面积。栈保存的是柱子的下标,而不是柱子的高度,目的是方便计算矩形的面积。遇到上升的柱子,就把柱子对应的下标压入栈。

代码

/*---------------------------------------
*   日期:2015-05-13
*   作者:SJF0115
*   题目: 84.Largest Rectangle in Histogram
*   网址:https://leetcode.com/problems/largest-rectangle-in-histogram/
*   结果:AC
*   来源:LeetCode
*   博客:
-----------------------------------------*/
#include <iostream>
#include <stack>
#include <vector>
using namespace std;

class Solution {
public:
    int largestRectangleArea(vector<int>& height) {
        int maxArea = 0;
        int size = height.size();
        if(size <= 0){
            return maxArea;
        }//if
        // 下标
        stack<int> indexStack;
        int top,width;
        for(int i = 0;i < size;++i){
            // 栈空或上升序列 压入栈
            if(indexStack.empty() || height[indexStack.top()] <= height[i]){
                indexStack.push(i);
            }//if
            // 一旦下降了计算面积
            else{
                top = indexStack.top();
                indexStack.pop();
                // 栈为空 表示从第一个到当前的最低高度
                width = indexStack.empty() ? i : (i - indexStack.top() - 1);
                maxArea = max(maxArea,height[top] * width);
                // 保持i的位置不变
                --i;
            }//else
        }//for
        // 计算剩余上升序列面积
        while(!indexStack.empty()){
            top = indexStack.top();
            indexStack.pop();
            width = indexStack.empty() ? size : (size - indexStack.top() - 1);
            maxArea = max(maxArea,height[top] * width);
        }//while
        return maxArea;
    }
};

int main(){
    Solution s;
    //vector<int> height = {2,1,5,6,2,3};
    //vector<int> height = {2,1};
    //vector<int> height = {1,2,3};
    //vector<int> height = {2,1,2};
    vector<int> height = {4,2,0,3,2,5};
    cout<<s.largestRectangleArea(height)<<endl;
    return 0;
}

运行时间

这里写图片描述

目录
相关文章
|
索引
LeetCode 84. Largest Rectangle in Histogram
给定n个非负整数表示直方图的条形高度,其中每个条形的宽度为1,找到直方图中最大矩形的区域。
73 0
LeetCode 84. Largest Rectangle in Histogram
Leetcode-Hard 84. Largest Rectangle in Histogram
Leetcode-Hard 84. Largest Rectangle in Histogram
99 0
Leetcode-Hard 84. Largest Rectangle in Histogram
[LeetCode] Largest Rectangle in Histogram
This link has a very neat code, which is rewritten below using stack since the push and pop operations of it are O(1) time, while the pop_back and push_back of vector tend to be more time-consuming.
819 0
|
28天前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
50 6
|
2月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
99 2
|
28天前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
2月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
47 7