【Leetcode】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.
 class Solution {
 public:
     int largestRectangleArea(vector<int> &height) {
         int max_area = ;
         stack<int> s;
         height.push_back();
         int i = ;
         while (i < height.size()) {
             if (s.empty() || height[s.top()] < height[i]) {
                 s.push(i++);
             } else {
                 int t = s.top();
                 s.pop();
                 max_area = max(max_area,
                     height[t] * (s.empty() ? i : i - s.top() - ));
             }
         }
         return max_area;
     }
 };
O(n2)的算法还是很好想的,但是如果借用数据结构--栈,可以使复杂度降低。
从左向右扫描数组,如果bar递增,则入栈,遇到第一个下降的bar时,开始出栈,计算从该bar(不含)到栈顶元素(含)之间形成的矩形面积,直到遇到栈里第一个低于它的bar,此时可以继续向前扫描下一个元素。
栈里维护的是递增序列。
【Leetcode】Largest Rectangle in Histogram的更多相关文章
- 【题解】Largest Rectangle in a Histogram [SP1805] [POJ2559]
		
[题解]Largest Rectangle in a Histogram [SP1805] [POJ2559] [题目描述] 传送: \(Largest\) \(Rectangle\) \(in\) ...
 - LeetCode 84. Largest Rectangle in Histogram 单调栈应用
		
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
 - 【LeetCode】Largest Number 解题报告
		
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
 - Java for LeetCode 084 Largest Rectangle in Histogram【HARD】
		
For example, Given height = [2,1,5,6,2,3], return 10. 解题思路: 参考Problem H: Largest Rectangle in a Hist ...
 - leetcode之Largest Rectangle in Histogram
		
问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...
 - 【leetcode】Maximal Rectangle
		
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
 - 关于LeetCode的Largest Rectangle in Histogram的低级解法
		
在某篇博客见到的Largest Rectangle in Histogram的题目,感觉蛮好玩的,于是想呀想呀,怎么求解呢? 还是先把题目贴上来吧 题目写的很直观,就是找直方图的最大矩形面积,不知道是 ...
 - 【leetcode刷题笔记】Largest Rectangle in Histogram
		
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
 - [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形
		
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
 
随机推荐
- JavaScript语言基础-对象与数组
 - 在云服务器上体验Docker
			
1. 添加Docker repository key sh -c "wget -qO- https://get.docker.io/gpg | apt-key add -" 2. ...
 - 问题:不支持Dictionary;结果:在Web Service中傳送Dictionary
			
在Web Service中傳送Dictionary 有個需求,想在Web Service中傳遞Dictionary<string, string>參數,例如: 排版顯示純文字 [WebMe ...
 - #测试两种不同的SVM,rbf的核真是太棒了(一种会拐弯的边界)
			
from sklearn import datasets import numpy as np X, y = datasets.make_blobs(n_features=2, centers=2) ...
 - c#抓取网页数据
			
写了一个简单的抓取网页数据的小例子,代码如下: //根据Url地址得到网页的html源码 private string GetWebContent(string Url) { string strRe ...
 - CORS实现跨域Ajax
			
客户端 #!/usr/bin/env python import tornado.ioloop import tornado.web class MainHandler(tornado.web.Req ...
 - css知多少(1)——我来问你来答(转)
			
css知多少(1)——我来问你来答 1. 引言 各位前端或者伪前端(比如作者本人)的同志们,css对你们来说不是很陌生.比如我,在几年之前上大学的时候,给外面做网站就用css,而且必须用css.这 ...
 - Arduino 003 Ubuntu(Linux) 系统下,如何给板子烧写程序
			
Ubuntu/Linux 系统下,如何给Arduino板子烧写程序 使用的虚拟机软件:VMware 11 我的Ubuntu系统:Ubuntu 14.04.10 TLS Arduino 软件的版本:Ar ...
 - 20169219《Linux内核原理与分析》第八周作业
			
网易云课堂学习 task_struct数据结构 struct task_struct { volatile long state;进程状态 void *stack; 堆栈 pid_t pid; 进程标 ...
 - 2015年第六届蓝桥杯省赛试题(JavaA组)
			
1.结果填空 (满分3分)2.结果填空 (满分5分)3.结果填空 (满分9分)4.代码填空 (满分11分)5.代码填空 (满分13分)6.结果填空 (满分17分)7.结果填空 (满分21分)8.程序设 ...