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 Solution 1: O(N^2) LTE
class Solution(object):
def largestRectangleArea(self, heights):
"""
:type heights: List[int]
:rtype: int
"""
if not heights:
return 0
res = -1
for i, height in enumerate(heights):
min_height = height
res = max(res, min_height)
for j in range(i + 1, len(heights)):
min_height = min(min_height, heights[j])
res = max(res, (j - i + 1) * min_height)
return res

Solution 2: O(N)

 class Solution(object):
def largestRectangleArea(self, heights):
"""
:type heights: List[int]
:rtype: int
"""
if not heights:
return 0
res, index = 0, 0
stack = []
while index <= len(heights):
cur = 0 if index == len(heights) else heights[index]
if not stack or cur >= heights[stack[-1]]:
stack.append(index)
index += 1
else:
height = heights[stack.pop()]
# make sure left and right index are correct
right = index - 1
left = 0 if not stack else stack[-1] + 1
res = max(res, (right - left + 1) * height)
return res

[LC] 84. Largest Rectangle in Histogram的更多相关文章

  1. 84. Largest Rectangle in Histogram

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

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

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

  3. 刷题84. Largest Rectangle in Histogram

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

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

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

  5. 【LeetCode】84. Largest Rectangle in Histogram

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

  6. 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形

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

  7. 84. Largest Rectangle in Histogram *HARD* -- 求柱状图中的最大矩形面积

    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 OJ 84. Largest Rectangle in Histogram

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

随机推荐

  1. Java 面向对象概述原理: 多态、Object类,转型(8)

    Java 面向对象概述原理: 多态.Object类,转型(8) http://docs.oracle.com/javase/tutorial/java/IandI/override.html Java ...

  2. tensorflow函数解析:Session.run和Tensor.eval

    原问题链接: http://stackoverflow.com/questions/33610685/in-tensorflow-what-is-the-difference-between-sess ...

  3. 用数组来实现Stack

    1:Stack特点 stack:栈,是一种特殊的数据结构,有着先入后出的特点(first in last out).stack中栈底始终不变,只有一端能变化.栈顶在有数据push的时候增大,有数据po ...

  4. PAT Advanced 1138 Postorder Traversal (25) [树的遍历,前序中序转后序]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and ...

  5. [Shoi2013]超级跳马(DP+矩阵乘法)

    设f[i][j]表示方案数,显然有一个O(m2n)的暴力DP法,但实际上可以按距离当前位置的奇偶性分成s1[i][j]和s2[i][j],然后这个暴力DP可以优化到O(nm)的暴力.于是有这样的递推式 ...

  6. Python笔记_第一篇_面向过程_第一部分_5.Python数据类型之元组类型(tuple)

    元组!在Python中元组是属于列表的一种延伸,也是一种有序集合,成为一种只读列表,即数据可以被查找,不能被修改,列表的切片操作同样适用于元组. 特点:1. 与列表非常相似. 2. 一旦初始化就不能修 ...

  7. 7.windows-oracle实战第七课 --约束、索引

    数据的完整性 数据的完整性用于确保数据库数据遵从一定的商业和逻辑规则.数据的完整性使用约束.触发器.函数的方法来实现.在这三个方法中,约束易于维护,具备最好的性能,所以作为首选.  约束:not nu ...

  8. 18)PHP,可变函数,匿名函数 变量的作用域

    (1)可变函数: 可变函数,就是函数名“可变”——其实跟可变变量一样的道理. $str1 = “f1”;   //只是一个字符串,内容为”f1” $v1 = $str1(3, 4);   //形式上看 ...

  9. 吴裕雄--天生自然 PYTHON3开发学习:函数

    def 函数名(参数列表): 函数体 # 计算面积函数 def area(width, height): return width * height def print_welcome(name): ...

  10. elasticsearch-hadoop 扩展定制 官方包以支持 update upsert doc

    官方源码地址https://github.com/elastic/elasticsearch-hadoop 相关文档 https://www.elastic.co/guide/en/elasticse ...