Skip to content

Commit 999a3b7

Browse files
committed
4
1 parent cea4092 commit 999a3b7

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
int largestRectangleArea(vector<int>& height) {
4+
// Start typing your C/C++ solution below
5+
// DO NOT write int main() function
6+
7+
int n = height.size();
8+
int width[n];
9+
stack<int> minHeight;
10+
11+
for (int i = 0; i < n; i++) {
12+
while (!minHeight.empty() && height[i] <= height[minHeight.top()])
13+
minHeight.pop();
14+
int left = minHeight.empty() ? -1 : minHeight.top();
15+
width[i] = i - left - 1;
16+
minHeight.push(i);
17+
}
18+
19+
while (!minHeight.empty())
20+
minHeight.pop();
21+
22+
for (int i = n - 1; i >= 0; i--) {
23+
while (!minHeight.empty() && height[i] <= height[minHeight.top()])
24+
minHeight.pop();
25+
int right = minHeight.empty() ? n : minHeight.top();
26+
width[i] += right - i - 1;
27+
minHeight.push(i);
28+
}
29+
30+
int result = 0;
31+
for (int i = 0; i < n; i++)
32+
result = max(result, height[i] * (width[i] + 1));
33+
return result;
34+
}
35+
};

0 commit comments

Comments
 (0)