-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLargestRectangleInHistogram.cpp
62 lines (49 loc) · 1.19 KB
/
LargestRectangleInHistogram.cpp
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
56
57
58
59
60
61
62
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int largestRectangleArea(vector<int> &heights)
{
int n = heights.size();
vector<int> left(n, 0); // left smaller nearst
vector<int> right(n, 0); // right smaller nearst
stack<int> s;
// right smaller
for (int i = n - 1; i >= 0; i--)
{
while (s.size() > 0 && heights[s.top()] >= heights[i]) // removing invlaid values
{
s.pop();
}
right[i] = s.empty() ? n : s.top();
s.push(i);
}
while (!s.empty()) // to remove previous stack element
{
s.pop();
}
// left smaller
for (int i = 0; i < n; i++)
{
while (s.size() > 0 && heights[s.top()] >= heights[i]) // removing invlaid values
{
s.pop();
}
left[i] = s.empty() ? -1 : s.top();
s.push(i);
}
int ans = 0;
for (int i = 0; i < n; i++)
{
int width = right[i] - left[i] - 1;
int currArea = heights[i] * width;
ans = max(ans, currArea);
}
return ans;
}
int main()
{
vector<int> arr = {2, 1, 5, 6, 2, 3};
cout << largestRectangleArea(arr) << endl;
return 0;
}