forked from kamyu104/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
largest-rectangle-in-histogram.cpp
41 lines (38 loc) · 1.45 KB
/
largest-rectangle-in-histogram.cpp
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
30
31
32
33
34
35
36
37
38
39
40
41
// Time: O(n)
// Space: O(n)
class Solution {
public:
/**
* @param height: A list of integer
* @return: The area of largest rectangle in the histogram
*/
int largestRectangleArea(vector<int> &height) {
stack<int> increasing_height;
int max_area = 0;
int i = 0;
while (i <= height.size()) { // "i == height.size()" is to handle remaning heights in stack.
if (increasing_height.empty() ||
(i < height.size() && height[i] > height[increasing_height.top()])) {
// Stores height in a strictly increasing order.
increasing_height.emplace(i);
++i;
} else {
// Compute all largest rectangles of heights in stack which height >= current height.
int last = increasing_height.top();
increasing_height.pop();
if (increasing_height.empty()) {
// Left bound: 0
// Right bound: i - 1
// Height: height[last]
max_area = max(max_area, height[last] * (i - 1 - (-1)));
} else {
// Left bound: increasing_height.top() + 1
// Right bound: i - 1
// Height: height[last]
max_area = max(max_area, height[last] * (i - 1 - increasing_height.top()));
}
}
}
return max_area;
}
};