-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargest_rectangle_84.cpp
More file actions
55 lines (50 loc) · 1.6 KB
/
largest_rectangle_84.cpp
File metadata and controls
55 lines (50 loc) · 1.6 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <vector>
#include <stack>
#include <iostream>
using namespace std;
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
// for each height, get the max width
int l = heights.size();
stack<int> smaller;
// for each element in heights, only need to care about the
// index of the smaller element closest to it. For further smaller element
// -> don't care because the width is already restricted
vector<int> left_close_small(l, -1);
vector<int> right_close_small(l, l);
smaller.push(0);
for (int i = 1; i < l; ++i) {
while (!smaller.empty() && heights[smaller.top()] >= heights[i]){
smaller.pop();
}
if (smaller.empty()) {
left_close_small[i] = -1;
} else {
left_close_small[i] = smaller.top();
}
smaller.push(i);
}
while (!smaller.empty()) {
smaller.pop();
}
smaller.push(l-1);
for (int i = l-2; i >= 0; --i) {
while (!smaller.empty() && heights[smaller.top()] >= heights[i]) {
smaller.pop();
}
if (smaller.empty()) {
right_close_small[i] = l;
} else {
right_close_small[i] = smaller.top();
}
smaller.push(i);
}
int res = 0;
for (int i = 0; i < l; ++i) {
res = std::max(res
, heights[i] * (right_close_small[i] - left_close_small[i]-1));
}
return res;
}
};