Question

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 = 10unit.

Example

Given height = [2,1,5,6,2,3],
return 10.

Solution 1 -- Naive

For each start point i

  For each end point j

    minHeight = min height between i and j

    result = max{result, (j - i + 1) * minHeight}

Time Complexity O(n2)

 public class Solution {
/**
* @param height: A list of integer
* @return: The area of largest rectangle in the histogram
*/
public int largestRectangleArea(int[] height) {
// write your code here
if (height == null || height.length < 1)
return 0;
int start, end, minHeight, result = Integer.MIN_VALUE;
for (start = 0; start < height.length; start++) {
minHeight = height[start];
for (end = start; end < height.length; end++) {
minHeight = Math.min(minHeight, height[end]);
result = Math.max(result, (end - start + 1) * minHeight);
}
}
return result;
}
}

Solution 2 -- Increasing Stack

根据木桶原理,面积由最矮的高度决定。我们把问题转换为

For 决定矩阵高度的那根最矮木头 i

  看 i 往左最远能延伸到什么地方 indexLeft

  看 i 往右最远能延伸到什么地方 indexRight

  best = max{best, height[i] * (indexRight - indexLeft + 1)}

所以我们要找:

往左走第一个比height[i]小的数

往右走第一个比height[i]小的数

这种题典型的用递增/递减栈实现。

 public class Solution {
/**
* @param height: A list of integer
* @return: The area of largest rectangle in the histogram
*/
public int largestRectangleArea(int[] height) {
// write your code here
if (height == null || height.length < 1)
return 0;
int result = 0;
Stack<Integer> increasingStack = new Stack<Integer>();
// Attention here i <= length
for (int i = 0; i <= height.length; i++) {
int currentValue = ((i == height.length) ? -1 : height[i]);
while (!increasingStack.isEmpty() && height[increasingStack.peek()] >= currentValue) {
int currentHeight = height[increasingStack.pop()];
int left = increasingStack.size() > 0 ? increasingStack.peek() : -1;
int right = i;
result = Math.max(result, (right - left - 1) * currentHeight);
}
increasingStack.push(i);
}
return result;
}
}

注意,这里除了要计算以每个pop出来的元素为高的最大面积,还要计算每个未被pop出来的元素为高的最大面积。因此技巧在于最后多加一个元素-1,由于输入元素均大于等于0,所以当-1要push入栈时,栈里所有的元素都会被pop出来。

还要注意,当前值等于栈顶值时也要做出栈操作。

每个元素只入栈/出栈一次,因此时间复杂度是O(1)

Largest Rectangle in Histogram 解答的更多相关文章

  1. 刷题84. Largest Rectangle in Histogram

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

  2. LeetCode 笔记系列 17 Largest Rectangle in Histogram

    题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...

  3. 47. Largest Rectangle in Histogram && Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  4. 【LeetCode】84. Largest Rectangle in Histogram

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  5. leetcode Largest Rectangle in Histogram 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...

  6. 关于LeetCode的Largest Rectangle in Histogram的低级解法

    在某篇博客见到的Largest Rectangle in Histogram的题目,感觉蛮好玩的,于是想呀想呀,怎么求解呢? 还是先把题目贴上来吧 题目写的很直观,就是找直方图的最大矩形面积,不知道是 ...

  7. leetcode之Largest Rectangle in Histogram

    问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...

  8. LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle

    1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...

  9. 84. Largest Rectangle in Histogram

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

随机推荐

  1. python在一个列表中查找

    # -*- coding: utf-8 -*-__author__ = 'Administrator'import bisect#简化一些操作#1:在一个列表中查找""" ...

  2. python倒计时

    #coding=utf-8 #!/usr/bin/env python import datetime,time i=3 while i==3: spring=datetime.datetime(20 ...

  3. css中的边框样式

    在盒子模型中,盒子的边框是其重要的样式,通过边框我们可以很方便地看出盒子的长宽以及大小.边框的特性可以通过边框线,边框的宽度及颜色来呈现. 1,边框线 边框线指的是边框线条的样式,包括实线,虚线,点划 ...

  4. Android项目中gen文件下R文件无法生成的解决的方法

    帮一个网友解决R文件无法生成的问题,搜集了些材料特整理例如以下,刚開始学习的人參考他人代码时极易出现此种问题,一般都是xml文件出错,无法被正确解析. gen文件夹无法更新,或者gen文件夹下的R.J ...

  5. 辛星解读mysql的用户管理

    可能做开发的多半不太关注这方面,可是要说到做运维.那就不能不关注了.由于我们都知道,root的权限太大了.不是随便能用的.我们平时最好用一些比較低的权限的用户.这样会让我们的安全性大大提高,也能防止我 ...

  6. windows下体验Redis

    Redis 是一个高性能的key-value数据库, 使用内存作为主存储,数据访问速度非常快,当然它也提供了两种机制支持数据持久化存储.比较遗憾的是,Redis项目不直接支持Windows,Windo ...

  7. Android-它们的定义Dialog

    Android-它们的定义Dialog 2014年4月27日 星期天 天气晴朗 心情平静 本篇博文来分享一个也是开发中常常须要用到的功能-自己定义对话框,这里我用到了Android中的图形资源shap ...

  8. install-file -Dfile=J:\project01\workspace\service\lib\javapns-jdk16-163.jar -DgroupId=org.json -Dar

    今天在开发项目的时候发现了一个问题,所以通过博客来记录起来! 为了以后在问题的解决方面能得到借鉴! 问题的现象是这种: 这样会报错的.pom.xml文件他在编译.检查他的文件语法的时候是须要參考库中的 ...

  9. php:兄弟连之面向对象版图形计算器1

    曾经看细说PHP的时候就想做这个,可是一直没什么时间,这次总算忙里偷闲搞了代码量比較多的project. 首先,文档结构,都在一个文件夹下就好了,我的就例如以下. 一開始,进入index.php文件. ...

  10. spark 高级算子

      mapPartitionsWithIndex val func = (index: Int, iter: Iterator[(Int)]) => {   iter.toList.map(x  ...