LeetCode:Largest Rectangle in Histogram(update)

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.

For example,
Given height = [2,1,5,6,2,3],
return 10.

别人家的巧妙的代码:每次都维护一个递增的栈 比较栈顶与当前元素。如果当前元素小于栈顶元素,则入栈,否则合并现有栈,直至栈顶元素小于当前元素。结尾时

入栈元素为0,重复合并一次。

 1 class Solution {
 2 public:
 3     int largestRectangleArea(vector<int>& height) {
 4
 5          stack<int> s;
 6
 7          int result=0;
 8          height.push_back(0);
 9
10          for(int i=0;i<height.size();i++)
11          {
12              if(s.empty()||height[s.top()]<height[i])
13                 s.push(i);
14               else{
15
16                      int index = s.top();
17                      s.pop();
18                      int width = s.empty() ? i : (i-s.top()-1);
19                      result = max(result,height[index] * width);
20                      i--;
21
22               }
23          }
24          return result;
25
26     }
27 };
时间: 2024-10-16 10:49:41

LeetCode:Largest Rectangle in Histogram(update)的相关文章

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

解题报告:LeetCode Largest Rectangle in Histogram(计算最大矩形面积)

题目出处:https://leetcode.com/problems/largest-rectangle-in-histogram/题意描述:给定n个非负的整数,代表n个依次相邻的宽度为1的柱形的高,求这些柱形所能形成的最大的矩形面积. 解决思路:此题最直接最原始的做法就是扫描起点和终点,并随时更新最大面积,但是这样的做法的复杂度为O(n^2),显然会超时,这里就不再贴代码了. 于是我们需要考虑怎么将复杂度降下来,一种想法是在求面积之前进行预处理,将每个整数左右的第一个比当前位置矮的柱形的下标l

Leetcode 之Largest Rectangle in Histogram(40)

又是一道构思巧妙的题,暴力求解复杂度太高,通过构造一个递增栈,O(n)就能解决问题,其中的过程值得细细体会. int largestRect(vector<int> &height) { stack<int> s;//定义一个单调递增栈 height.push_back(0);//定义单调递增栈的最后一个 int result = 0;//记录当前最大的面积 for (int i = 0; i < height.size();)//满足条件i才递增 { if (s.e

(每日算法)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

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 &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

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

(数组)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

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