-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0084.cpp
More file actions
31 lines (28 loc) · 836 Bytes
/
Copy path0084.cpp
File metadata and controls
31 lines (28 loc) · 836 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
class Solution {
public:
int largestRectangleArea(vector<int> &heights) {
int n = heights.size();
vector<int> left(n), right(n);
stack<int> st;
for (int i = 0; i < n; i++) {
left[i] = i;
while (!st.empty() && heights[st.top()] >= heights[i]) {
left[i] = left[st.top()];
st.pop();
};
st.push(i);
}
for (int i = n - 1; i >= 0; i--) {
right[i] = i;
while (!st.empty() && heights[st.top()] >= heights[i]) {
right[i] = right[st.top()];
st.pop();
};
st.push(i);
}
int res = 0;
for (int i = 0; i < n; i++)
res = max(res, (right[i] - left[i] + 1) * heights[i]);
return res;
}
};