-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxRectangle.hpp
More file actions
35 lines (30 loc) · 864 Bytes
/
MaxRectangle.hpp
File metadata and controls
35 lines (30 loc) · 864 Bytes
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
#include <utility>
#include <vector>
/**
* @brief Solve max rectangle in histogram problem.
* @param v All the element must be non-negative.
*/
template <typename T>
T max_rectangle(std::vector<T> v) {
// push sentinel
v.emplace_back();
int n = (int)v.size();
// (height, pos). height is strictly increasing after each loop.
std::vector<std::pair<T, int>> st;
st.reserve(n);
T mxv{};
for (int i = 0; i < n; i++) {
if (st.empty() || st.back().first < v[i]) {
st.emplace_back(v[i], i);
continue;
}
int last = i;
while (!st.empty() && st.back().first >= v[i]) {
mxv = std::max(mxv, st.back().first * (i - st.back().second));
last = st.back().second;
st.pop_back();
}
st.emplace_back(v[i], last);
}
return mxv;
}