-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path84.cpp
More file actions
44 lines (41 loc) · 935 Bytes
/
Copy path84.cpp
File metadata and controls
44 lines (41 loc) · 935 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
36
37
38
39
40
41
42
43
44
#include "common.h"
using namespace std;
class Solution {
public:
int largestRectangleArea(vector<int> &heights) {
int n = heights.size();
vector<int> l(heights.size(), -1);
vector<int> r(heights.size(), n);
stack<int> s;
for (auto i = 0; i < n; ++i) {
while (!s.empty() && heights[s.top()] >= heights[i]) {
s.pop();
}
if (!s.empty()) {
l[i] = s.top();
}
s.push(i);
}
s = stack<int>();
for (auto i = n - 1; i >= 0; --i) {
while (!s.empty() && heights[s.top()] >= heights[i]) {
s.pop();
}
if (!s.empty()) {
r[i] = s.top();
}
s.push(i);
}
int max = 0;
for (auto i = 0; i < n; ++i) {
max = std::max(max, heights[i] * (r[i] - l[i] - 1));
}
return max;
}
};
int main() {
Solution s;
vector<int> v = {2, 1, 5, 6, 2, 3};
cout << s.largestRectangleArea(v) << endl;
return 0;
}