作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址: https://leetcode.com/problems/largest-rectangle-in-histogram/description/

题目描述

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

题目大意

计算一堆矩形能够成的面积最大的一块是多少。

解题方法

单调栈

这个题是单调栈的运用,使用一个单调递增栈来维护已经出现了的矩形高度。

  1. 如果后面新来的元素高度比栈顶元素高,那么需要入栈,因为面积最大的元素会出现在后面。
  2. 如果后面新来的元素高度比栈顶元素小,那么需要弹出栈里的元素,并且,每次弹出的时候都要对计算目前的宽度,相乘得到面积。

栈里保存索引的方式是需要掌握的,保存索引的方式在最小值栈结构中也有运用。

每次求栈内矩形的宽度的时候,其实是求其位置到最右边的距离。注意即将入栈的元素索引 i 是一直不变的,另外栈里的每个元素的索引可以认为是矩形的右边界。

Leetcode #84 Largest Rectangle in Histogram图文并茂,讲得很清楚。

最坏时间复杂度是O(N^2),最优时间复杂度是O(N),空间复杂度是O(N)。

class Solution(object):
def largestRectangleArea(self, heights):
"""
:type heights: List[int]
:rtype: int
"""
stack = list()
res = 0
heights.append(0)
N = len(heights)
for i in range(N):
if not stack or heights[i] > heights[stack[-1]]:
stack.append(i)
else:
while stack and heights[i] <= heights[stack[-1]]:
h = heights[stack[-1]]
stack.pop()
w = i if not stack else i - stack[-1] - 1
res = max(res, h * w)
stack.append(i)
return res

参考资料:

https://www.cnblogs.com/boring09/p/4231906.html

日期

2018 年 10 月 9 日 —— 今天降温7度,注意保暖

【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)的更多相关文章

  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

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

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

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

  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. 084 Largest Rectangle in Histogram 柱状图中最大的矩形

    给出 n 个非负整数来表示柱状图的各个柱子的高度,每个柱子紧挨彼此,且宽度为 1 .您的函数要能够求出该柱状图中,能勾勒出来的最大矩形的面积. 详见:https://leetcode.com/prob ...

  9. 84. Largest Rectangle in Histogram

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

随机推荐

  1. linux安全性增加

    账户安全问题 Linux  默认会安装很多不必要的用户和用户组,如果不需要某些用户或者组,就要立即删除它,因为账户越多,系统就越不安全,很可能被黑客利用,进而威胁到服务器的安全. Linux系统中可以 ...

  2. C#表格,表格信息、GridView使用。

    page: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="test1.a ...

  3. addict, address, adequate

    addict Addiction is a biopsychosocial disorder characterized by repeated use of drugs, or repetitive ...

  4. 零基础学习java------day14-----泛型,foreach,可变参数,数组和集合间的转换,Set,Map,

    1.泛型(jdk1.5以后出现) https://www.cnblogs.com/lwbqqyumidi/p/3837629.html#!comments (1)为什么要用泛型? 限制集合,让它只能存 ...

  5. ORACLE 本session产生的redo

    select * from v$statname a ,v$mystat bwhere a.STATISTIC# = b.STATISTIC# and a.name = 'redo size';

  6. hadoop Sort排序

    1 public int getPartition(IntWritable key,IntWritable value,int numPartitions){ 2 int Maxnumber = 12 ...

  7. Enumeration遍历http请求参数的一个例子

    Enumeration<String> paraNames=request.getParameterNames(); for(Enumeration e=paraNames;e.hasMo ...

  8. GCD的补充

    1-1 关于GCD中的创建和释放     在iOS6.0之前,在GCD中每当使用带creat单词的函数创建对象之后,都应该对其进行一次release操作.           在iOS6.0之后,GC ...

  9. Dubbo中CompletableFuture异步调用

    使用Future实现异步调用,对于无需获取返回值的操作来说不存在问题,但消费者若需要获取到最终的异步执行结果,则会出现问题:消费者在使用Future的get()方法获取返回值时被阻塞.为了解决这个问题 ...

  10. 南京邮电大学CTF密码学之MD5-golang与php代码实现

    题目内容:这里有一段丢失的md5密文 e9032???da???08????911513?0???a2 要求你还原出他并且加上nctf{}提交 已知线索 明文为: TASC?O3RJMV?WDJKX? ...