-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxAreaHistogram2.cpp
More file actions
43 lines (41 loc) · 1015 Bytes
/
Copy pathMaxAreaHistogram2.cpp
File metadata and controls
43 lines (41 loc) · 1015 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
32
33
34
35
36
37
38
39
40
41
42
43
//TC = O(n) single pass
//SC = o(n)
#include <bits/stdc++.h>
#include <vector>
#include <string>
#include <math.h>
using namespace std;
#define ll long long
#define IOS ios_base::sync_with_stdio(false);
#define print(array) \
for (auto it : array) \
cout << it << " "
#define sortit(array) sort(array.begin(), array.end())
const int M = 1e9 + 7;
int largestRectangleArea(vector<int> &heights)
{
stack<int> st;
int maxarea = 0;
int width = 0;
int n = heights.size();
for (int i = 0; i <= n; i++)
{
while (!st.empty() && (i == n || heights[st.top()] >= heights[i]))
{
int height = heights[st.top()];
st.pop();
if (st.empty())
width = i;
else
width = i - st.top() - 1;
maxarea = max(maxarea, width * height);
}
st.push(i);
}
return maxarea;
}
int main()
{
vector<int> heights = {2, 1, 5, 6, 2, 3};
cout << largestRectangleArea(heights);
}