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