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.

Example:

Input: [2,1,5,6,2,3]
Output: 10

题意:

给定一个直方图,求其能覆盖的最大矩形。

思路:

以下是O(n2) 的时间复杂度解法

当 i=  0       1   2   3   4

area= 2*1    4*1    6*1    5*1    3*1

2*2    4*2   5*2    3*2

2*3   4*3    3*3

2*4    3*4

2*5

以height[i]为临界,每次比较其左侧的所有height谁更小,更新minHeight, 更新当前maxArea

 class Solution {
public int largestRectangleArea(int[] heights) {
int maxArea = 0;
for(int i = 0; i < heights.length; i++){
int minHeight = heights[i]; //以height[i]为临界
for(int j = i; j >= 0; j--){ // 每次比较其左侧的所有height谁更小
minHeight = Math.min(minHeight, heights[j]);
maxArea = Math.max(maxArea, minHeight * (i - j + 1));
}
}
return maxArea;
}
}

以下是O(n) 的时间复杂度解法

维护一个单调栈monoStack,数组中每个元素的index都入栈、出栈

代码:

 class Solution {
public int largestRectangleArea(int[] heights) {
Stack<Integer> s = new Stack<>();
int area = 0;
for(int i=0; i<= heights.length;){
int value = i< heights.length ? heights[i]: 0;
if(s.isEmpty() || value> heights[s.peek()]){
s.push(i);
i++;
}else{
int temp = s.pop();
area = Math.max(area, heights[temp] * (s.isEmpty() ? i: (i-s.peek()-1)));
} }
return area;
}
}

[leetcode]84. Largest Rectangle in Histogram直方图中的最大矩形的更多相关文章

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

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

  2. [LeetCode] 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 单调栈应用

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

  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 (最大矩形直方图) 解题思路和方法

    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 ,O(n)解法剖析

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

  8. [LeetCode#84]Largest Rectangle in Histogram

    Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...

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

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

随机推荐

  1. C# ZipHelper C#公共类 -- ZipArchive实现压缩和解压

    从网上找来个ZipArchive来压缩和解压缩的类,供参考吧 /******************************************************************** ...

  2. MySQL Key值(PRI, UNI, MUL)的含义

    PRI主键约束: UNI唯一约束: MUL可以重复. 参考:http://www.cnblogs.com/licheng/archive/2010/10/16/1852938.html

  3. git中出现remote: HTTP Basic: Access denied

    git中出现remote: HTTP Basic: Access denied 1.git clone时出现 Username for 'http://******': *** remote: HTT ...

  4. Excel技巧--巧用分列功能整理日期格式

    遇到这样混乱的日期列表,可以使用“分列”功能来整理: 1.选择该列,点击“数据”-->“分列”功能: 2.在对话框中的第1.2步都不用设置,到第3步选择“日期”格式: 4.完成后,再使用单元格格 ...

  5. [转][C#]文件流读取

    { internal static class FileUtils { public static string GetRelativePath(string absPath, string base ...

  6. [UE4]Spin Box,数字输入,可拖动

    一.Spin Box在Input组下 二.Spin Box的文字样式可以在Spin Box.Display中修改 三.Spin Box事件 1.On Value Changed:值改变时触发 2.On ...

  7. linux下自定义dubbo的shell脚本

  8. Java 身份证号码验证

    身份证号码验证 1.号码的结构 公民身份号码是特征组合码,由十七位数字本体码和一位校验码组成.排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码 2.地址码(前 ...

  9. 46.Scrapy框架结构

    Scrapy的介绍:Scrapy是基于Twisted的异步处理框架,是纯python语言实现的爬虫框架,特点是架构清晰,模块间耦合度低.扩展性强较为灵活. 框架结构如图所示: Engine:引擎,处理 ...

  10. 【学习】DataFrame&Series类【pandas】

    参考链接:http://blog.csdn.net/yhb315279058/article/details/50226027 DataFrame类: DataFrame有四个重要的属性: index ...