84. Largest Rectangle in Histogram
来源:互联网 发布:中文域名重要性 编辑:程序博客网 时间:2024/06/03 15:45
原网址https://leetcode.com/problems/largest-rectangle-in-histogram/description/
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
.
代码
class Solution {public: int largestRectangleArea(vector<int>& heights) { stack<int> index; int maxArea = 0; int i = 0; while (i < heights.size()) { if (index.empty() || heights[index.top()] <= heights[i]) { index.push(i++); } else { int j = index.top(); index.pop(); int width = index.empty() ? i : i - index.top() - 1; maxArea = max(maxArea, heights[j] * width); } } while (!index.empty()) { int j = index.top(); index.pop(); int width = index.empty() ? i : i - index.top() - 1; maxArea = max(maxArea, heights[j] * width); } return maxArea; }};
阅读全文
0 0
- 84. Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- 84. largest rectangle in histogram
- 84. Largest Rectangle in Histogram
- 84.Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- 84. Largest Rectangle in Histogram
- 自动化测试的主要分类
- java中的POJO是什么意思?
- C++矩阵转置
- 数组Array的基本操作,进阶操作,多维数组
- 小试牛刀——用python画一朵美丽的玫瑰花
- 84. Largest Rectangle in Histogram
- 将一个视频转换为一个极坐标视频
- ThinkPhp 5 目录结构及基础配置详解
- 2017.12.10《“剑锋OI”普及组多校联盟系列赛(14)#Sooke#Kornal 的课余时间 》分析报告
- 通讯录改进(可以保存至文件并读取)
- kuangbin专题五 并查集 POJ 1213 How Many Tables
- Gossip算法学习笔记
- bonnie++安装报错zcav.cpp:
- 安装虚拟机教程(学习汇编语言)