-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargest-rectangle-in-histogram.cpp
More file actions
34 lines (31 loc) · 1.02 KB
/
largest-rectangle-in-histogram.cpp
File metadata and controls
34 lines (31 loc) · 1.02 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
class Solution {
public:
vector<pair<int, int> > memoLeft, memoRight;
int largestRectangleArea(vector<int> &bar){
int n = (int) bar.size();
vector<int> left(n), right(n);
memoLeft.push_back({-1, -1});
for (int i = 0; i < n; i++) {
int x = bar[i];
while(x <= memoLeft.back().first){
memoLeft.pop_back();
}
left[i] = memoLeft.back().second + 1;
memoLeft.push_back({x, i});
}
memoRight.push_back({-1, n});
for (int i = n - 1; i >= 0; i--) {
int x = bar[i];
while(x <= memoRight.back().first){
memoRight.pop_back();
}
right[i] = memoRight.back().second - 1;
memoRight.push_back({x, i});
}
int maxSoFar = 0;
for (int i = 0; i < n; i++) {
maxSoFar = max(maxSoFar, (right[i] - left[i] + 1) * bar[i]);
}
return maxSoFar;
}
};