-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestRectangleInHistogram.cc
More file actions
39 lines (35 loc) · 1.04 KB
/
LargestRectangleInHistogram.cc
File metadata and controls
39 lines (35 loc) · 1.04 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
30
31
32
33
34
35
36
37
38
39
namespace LargestRectangleInHistogram {
class Solution {
public:
int largestRectangleArea(vector<int> &height) {
if (height.empty()) {
return 0;
}
stack<int> S;
int maxArea = 0;
int i = 0;
while (i < height.size()) {
if (S.empty() || height[S.top()] <= height[i]) {
S.push(i);
i++;
} else {
int cur = S.top();
S.pop();
int curArea = height[cur] * (S.empty()? i : i - S.top() - 1);
if (curArea > maxArea) {
maxArea = curArea;
}
}
}
while (!S.empty()) {
int cur = S.top();
S.pop();
int curArea = height[cur] * (S.empty()? i : i - S.top() - 1);
if (curArea > maxArea) {
maxArea = curArea;
}
}
return maxArea;
}
};
}