084 Largest Rectangle In Histogram

这道题有两个做法 总体来说都是O(n) 但是第二个方法是第一个的简化版

方法一:

l[i] = max(j) 其中a[j], a[j+1], ...,a[i-1] < a[i]

r[i] = min(j) 其中 a[i] > a[i+1], ... ,a[j]

dif[i] = r[i] - l[i]

area[i] = a[i] * dif[i]

最大的area[i] 即为答案

a[i] : -inf, 2, 3, 1, 4, 6, 5, 7, 3, 2, -inf

i     :    0, 1 ,2, 3, 4, 5, 6, 7, 8, 9, 10

l[i]  :     , 0, 1, 0, 3, 4, 4, 6, 3, 3,

r[i]  :     , 3, 3,10, 8, 6, 8,8, 9,10

dif[i]:     , 3, 2, 10, 5, 2, 4, 2, 6, 7

area[i]:   ,4, 3, 9, 16, 6, 15, 7, 15, 12

方法二:

是第一种方法的改良, 使用stack, 在stack中记录a[i] 及 l[i]

具体代码如下

 1 class Solution:
 2     # @param {integer[]} height
 3     # @return {integer}
 4     def largestRectangleArea(self, height):
 5         ans = 0
 6         s = []
 7         for i in range(0, len(height)):
 8             left = i
 9             while (s != []) and (s[-1][0] > height[i]):
10                 left = s[-1][1]
11                 ans = max(ans, (i - left) * s[-1][0])
12                 s.pop()
13             s.append([height[i], left])
14         right = len(height)
15         while s != []:
16             left = s[-1][1]
17             ans = max(ans, (right - left) * s[-1][0])
18             s.pop()
19         return ans
时间: 2024-10-11 13:41:57

084 Largest Rectangle In Histogram的相关文章

Java for LeetCode 084 Largest Rectangle in Histogram【HARD】

For example, Given height = [2,1,5,6,2,3], return 10. 解题思路: 参考Problem H: Largest Rectangle in a Histogram第四种思路,或者翻译版Largest Rectangle in [email protected],JAVA实现如下: public int largestRectangleArea(int[] height) { Stack<Integer> stk = new Stack<In

LeetCode: Largest Rectangle in Histogram [084]

[题目] 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 lar

(每日算法)Leetcode -- Largest Rectangle in Histogram(最大实心矩形)

思路:如果时间复杂度要求是O(n 2 )的话,解法比较多也比较好理解.比如可以遍历,对于当前 i 位置上的立柱,计算出以这个i 立柱结尾的最大矩形,然后求出总的最大矩形. 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

(数组)Largest Rectangle in Histogram(栈解问题)

Largest Rectangle in Histogram 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 heigh

84. Largest Rectangle in Histogram *HARD* 柱状图求最大面积 85. Maximal Rectangle *HARD*

1. 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 large

Leetcode:Largest Rectangle in Histogram 最大矩形面积

Largest Rectangle in Histogram 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 heigh

C++]LeetCode: 133 Largest Rectangle in Histogram(最大矩形面积)

题目: 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 larg

LeetCode &quot;Largest Rectangle in Histogram&quot; - TRICKY MONO-QUEUE

I got a DP solution first which is O(n^2). Apparently it is not a optimized one - think about: it is linear value space. There must be O(n) solution. And yes there is: http://fisherlei.blogspot.com/2012/12/leetcode-largest-rectangle-in-histogram.html

【leetcode刷题笔记】Largest Rectangle in Histogram

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