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. [Java] 方法 -- 繼承關係

    public class test { void show() { System.out.println("父類別"); } } public class test2 extend ...

  2. 如何在Python中使用ZeroMQ和Docker构建微服务架构

    @Container容器技术大会将于6月4日在上海光大会展中心国际大酒店举办,来自携程.PPTV.蚂蚁金服.京东.浙江移动.海尔电器.唯品会.eBay.道富银行.麻袋理财等公司的技术负责人将带来实践经 ...

  3. C#的发展历程 -- 系列介绍

    C#的发展历程第五 - C# 7开始进入快速迭代道路 C#与C++的发展历程第四 - C#6的新时代 C#与C++的发展历程第三 - C#5.0异步编程巅峰 C#与C++的发展历程第二 - C#4.0 ...

  4. windows server 修改远程桌面连接端口号

    1. [运行]输入 regedit 2.  在注册表编辑器中找到以下PortNamber键,改为要使用的远程端口,如10000. HKEY_LOCAL_MACHINE\SYSTEM\CurrentCo ...

  5. Mysql常见的优化策略

    数据库设计方面优化 1.数据库设计符合第三范式,为了查询方便可以有一定的数据冗余.2.选择数据类型优先级 int > date,time > enum,char>varchar &g ...

  6. IndentationError:expected an indented block错误解决

    Python语言是一款对缩进非常敏感的语言,给很多初学者带来了困惑,即便是很有经验的Python程序员,也可能陷入陷阱当中.最常见的情况是tab和空格的混用会导致错误,或者缩进不对,而这是用肉眼无法分 ...

  7. Caused by: io.protostuff.ProtobufException: Protocol message contained an invalid tag (zero).

    [ERROR] com.xxxx.redis.RedisClientTemplate.getOject(RedisClientTemplate.java:60):http-bio-8080-exec- ...

  8. windows time-wait 问题处理记录

    问题描述:有一段时间,服务器启动了好多程序,做的是 obd监听服务,连接好多个服务器,由于程序的本身的问题造成大量的wait-time,一番百度后找到找到方案1 设置一由于wait-time 需要经过 ...

  9. vue中.sync 修饰符

    一直以来,都不太明白.sync的用法,归根结底原因在于,没有仔细阅读“.sync修饰符”. 正好,最近在拿一个项目练手,然后使用了elment-ui,然后在用到dialog的时候,属性visible是 ...

  10. python中的第三方日志模块logging

    基本上每个系统都有自己的日志系统,可以使自己写的,也可以是第三方的.下面来简单介绍一下python中第三方的日志模块,入手还是比较简单的,但是也很容易给自己埋雷. 下面是我参考的资料链接 入手demo ...