-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargest_rect_in_histogram.cpp
More file actions
29 lines (28 loc) · 1.43 KB
/
largest_rect_in_histogram.cpp
File metadata and controls
29 lines (28 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
***Problem Desc***: given bar heights in a histogram, what would be the largest rectangle that can be fit in the bars
- ***store the height and from which index that height is possible [O(n) time | O(n) space]***:
- stack stores pairs -> height, index -> the pair indicates a rectangle of height h can be formed from index i
- pop from stack when the top has a higher height than curr height since that rectangle can no longer be achieved
- push in the curr height and the index would be that of the last rectangle evicted from the stack or that of the elem itself
- trick: to avoid special while loop at end, add a 0 element to heights
*/
int largestRectangleArea(vector<int>& heights) {
heights.push_back(0);
int ans = 0, n = heights.size();
stack<pair<int,int>> indexStack;
for (int i = 0; i<n; ++i) {
int start = i;
ans = max(ans, heights[i]);
while (!indexStack.empty() && indexStack.top().first > heights[i]) {
ans = max(ans, indexStack.top().first*(i-indexStack.top().second));
start = indexStack.top().second;
indexStack.pop();
}
indexStack.push({heights[i], start});
}
return ans;
}
/*
***store index of left and right smaller for each bar [O(n) time | O(n) space]***:
- then for each bar calc the rectangle of its height since it can be formed from the point its left smaller till its right smaller was seen
*/