84. Largest Rectangle in Histogram
Problem 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:
1
2Input: [2,1,5,6,2,3]
Output: 10
1 | class Solution { |
Second time
This is a relatively straightforward solution. Use a pointer to find toward right, if a histogram i
is greater than i+1
, then we find a left boundary of this histogram i
. At this time, we traverse back toward left, find every minimum height
and multiply with the width to get the area.
指針往右遍歷, 當height[i]< height[i+1]: keep go right, 因如果後面比較高, 則maximum area 至少包含後面的 histogram.
example:[2,4,6,5,3]
- reach 6, next is 5.
minimum height* width= area6*1= 6 4*2= 8 2*3= 6
- reach 5, next is 3, smaller than 5.
5*1= 5 5*2= 10 4*3= 12 2*4= 8
- reach 3, end condition
3*1= 3 3*2= 6 3*3= 9 3*4= 12 2*5= 10
- max area is 12
#Solution:
//O(n^2)
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int res= 0;
for(int i= 0; i< heights.size(); i++){ //go toward left
if(i== heights.size()-1 || heights[i]> heights[i+1]){ //reached end || height[i]> heigh[i+1]
int minh= heights[i];
for(int j= i; j>=0; j--){ //go toward right, find every rectangle
minh= min(minh, heights[j]);
int area= minh* (i-j+1);
res= max(res, area);
}
}
}
return res;
}
};
Reference:
https://goo.gl/JusVW4
leetcode.com/problems/maximal-rectangle/discuss/29054/Share-my-DP-solution/27983