Problem:

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.

Analysis:

This problem is very very tirkcy, but elegnat!

The idea behind the solution is super powerful, you should clearly think about it.
Possible solution:
For each element in the height array.
1. go right until nums[left] < cur_height
2. then go left until nums[right] < cur_height
Then caculated the area of rectanlge of the encolsed region: cur_height * [(right-1) - (left-1) - 1]
They key point for each element is to find out the left broader and right broader.
Apparently, this method would take O(n^2). Can we do it more smartly?
iff height[cur-1]'s boundary is clear, if we still compare all elements before height[cur-1], which were already compared by height[cur-1]. There must be way to avoid those uncessary comparison. Algorithm:
Take advantage of a stack, the stack's elments are always in the "small-to-large" order.
Step 1: scan the height array from left to right.
(While) Once the current element <= top element at the stack. we pop out an element.
for (int i = 0; i < height.length; i++) {
while (!stack.isEmpty() && height[i] <= height[stack.peek()]) {
...
}
Note: this can guarantee all elements in the stack array in the ascending order. For each poped element, we can get its left border and right border, through following way.
1. the stack still have elements.
(not included)left border: the current top element in the stack, since it is the first element smaller than the poped element.
(not included)right border: the current element height[i]. Since only when "height[i] <= height[stack.peek()]", the poped element appears. The rectangle's area: (right border-1) - (left border+1) + 1 = (i - 1) - (stack.peek()+1) + 1 = (i - stack.peek() + 1) * cur_height. 2. the stack does not have elements.
(included)left border: It means all elements appear before the poped element are actually larger than the poped element, we could start from the first element of the array.
(not included)right border: The current element height[i]. Since only when "height[i] <= height[stack.peek()]", the poped element appears. The rectangle's area : (right border-1) - (left border) + 1 = (i - 1) - (0) + 1 = i * cur_height Implementation:
-------------------------------------------------------------------------------------------------
for (int i = 0; i < height.length; i++) {
while (!stack.isEmpty() && height[i] <= height[stack.peek()]) {
int cur_height = height[stack.pop()];
cur_max = stack.isEmpty() ? i*cur_height : (i-stack.peek()-1)*cur_height;
max = Math.max(max, cur_max);
}
stack.push(i);
}
------------------------------------------------------------------------------------------------- One idea should be kept in mind:
When we push a element into the stack, we pop out all elements larger than it.
Thus when we need to get left broder, we can directly get from stack.peek() after the poped operation. Since for all elements appeared after the pushed element, must take the pushed element as left border. Those poped elements would not affect them. <What a great idea!> Note: In this problem, we actually set one 0 at left side and one 0 at right side of the height array.
0 [height] 0.
For the above question, we must take care the case when all height elements were scaned, but there are still elements in the stack, thus we must use the right fake border. nums[height] = 0.

Solution:

public class Solution {
public int largestRectangleArea(int[] height) {
if (height == null || height.length == 0)
return 0;
Stack<Integer> stack = new Stack<Integer> ();
int max = 0, cur_max = 0;
for (int i = 0; i < height.length; i++) {
while (!stack.isEmpty() && height[i] <= height[stack.peek()]) {
int cur_height = height[stack.pop()];
cur_max = stack.isEmpty() ? i*cur_height : (i-stack.peek()-1)*cur_height;
max = Math.max(max, cur_max);
}
stack.push(i);
}
while (!stack.isEmpty()) {
int cur_height = height[stack.pop()];
cur_max = stack.isEmpty() ? height.length*cur_height : (height.length-stack.peek()-1)*cur_height;
max = Math.max(max, cur_max);
}
return max;
}
}

[LeetCode#84]Largest Rectangle in Histogram的更多相关文章

  1. LeetCode 84. Largest Rectangle in Histogram 单调栈应用

    LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...

  2. [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  3. LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形

    原题 Given n non-negative integers representing the histogram's bar height where the width of each bar ...

  4. [leetcode]84. Largest Rectangle in Histogram直方图中的最大矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  5. leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  6. [leetcode]84.Largest Rectangle in Histogram ,O(n)解法剖析

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  7. 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...

  8. 84. Largest Rectangle in Histogram

    https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...

  9. 刷题84. Largest Rectangle in Histogram

    一.题目说明 题目84. Largest Rectangle in Histogram,给定n个非负整数(每个柱子宽度为1)形成柱状图,求该图的最大面积.题目难度是Hard! 二.我的解答 这是一个 ...

随机推荐

  1. 31、三层架构、AJAX+FormsAuthentication实现登陆

    三层架构 前段时间公司要求修改一个网站,打开后我疯了,一层没有都是调用的DB接口,遍地的SQL语句,非常杂乱. 什么是三层架构? 三层架构是将整个项目划分为三个层次:表现层.业务逻辑层.数据访问层.目 ...

  2. (转) Spring读书笔记-----部署我的第一个Spring项目

    一.Spring介绍 Spring是一个轻量级的Java EE容器,它也是一种从实际需求出发,着眼于轻便,灵活,易于开发,易测试和易部署的轻量级开发框架.Spring它完成了大量开发中的通用步骤,留给 ...

  3. C#语法糖之第五篇: 泛型委托- Action<T>

    因为工作的原因(其实还是个人的惰性)昨天没有给大家分享文章,然后这几天也有很多园友也提出了他们报告的意见及指导,再次感谢这些兄弟们的照顾我 和支持,这个分类的文章我当时想的是把我的学习经验和工作中用到 ...

  4. Lucida Grande字体无法正常显示冒号的解决方案

    曾经贪图Mac OSX的UI漂亮,后来查到它用的是Lucida Grande字体,所以索性将win7也改成了那种字体,结果浏览器中的中文冒号全都显示为一个奇怪的符号.后来即使将字体设置回去也无法还原. ...

  5. IIS应用地址池监控

    目的:公司服务器IIS有十几个应用地址池,总在不经意间停掉一个,停止线系统日志里会有一大堆警告日志,然后就停掉了,分析了好几次,网上有人说是某一个网站的问题应该查网站, 但是网站又有那么多地址,谁知道 ...

  6. Synchronized vs SyncRoot

    我们知道,在.net的一些集合类型中,譬如Hashtable和ArrayList,都有Synchronized静态方法和SyncRoot实例方法,他们之间有联系吗?我怎么才能用好他们呢?我们以Hash ...

  7. LINUX nohup命令输入输出深浅进出

    无论是否将 nohup命令的输出重定向到终端,输出都将附加到当前目录的 nohup.out 文件中.如果当前目录的 nohup.out 文件不可写,输出重定向到 $HOME/nohup.out 文件中 ...

  8. struts通过Ajax返回数据时,例如对象类型,没有执行Ajax的回调函数

    <result type="json"  name="success">                 <param name=" ...

  9. SVM技法

    PLA不管胖瘦,SVM喜欢胖的 fewer dichotomies=> small VC 演算法的VC dimension shatter 掉3个点 如果限制胖瘦,两个点都shatter不掉 喜 ...

  10. js隔行变色

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...