-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlargest-rectangle-in-histogram.cpp
More file actions
37 lines (27 loc) · 1.04 KB
/
largest-rectangle-in-histogram.cpp
File metadata and controls
37 lines (27 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
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
if (heights.empty()) {
return 0;
}
std::vector<std::pair<int,int>> vec;
std::transform(heights.begin(),heights.end(), std::back_inserter(vec), [](int a) {return std::make_pair(a,-a);});
go(vec);
std::reverse(vec.begin(),vec.end());
go(vec);
return std::max_element(vec.begin(), vec.end(), [](auto a, auto b){return a.second < b.second;})->second;
}
void go(std::vector<std::pair<int,int>>& vec) {
std::stack<std::pair<int,int>> stk;
stk.push(std::make_pair(-1,-1));
const int n = vec.size();
for (int i = 0; i < n; ++i) {
while (stk.top().first >= vec[i].first) {
stk.pop();
}
assert(stk.size());
vec[i].second += vec[i].first * (i - stk.top().second);
stk.push({vec[i].first, i});
}
}
};