-
Notifications
You must be signed in to change notification settings - Fork 0
/
greatest_areaHIstogram.cpp
62 lines (57 loc) · 1.58 KB
/
greatest_areaHIstogram.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<bits/stdc++.h>
using namespace std;
class Solution {
private:
vector<int> nextSmallerElement(long long arr[], int n) {
stack<int> s;
s.push(-1);
vector<int> ans(n);
for(int i = n - 1; i >= 0; i--) {
long long curr = arr[i];
while(s.top() != -1 && arr[s.top()] >= curr) {
s.pop();
}
ans[i] = s.top();
s.push(i);
}
return ans;
}
vector<int> prevSmallerElement(long long arr[], int n) {
stack<int> s;
s.push(-1);
vector<int> ans(n);
for(int i = 0; i < n; i++) {
long long curr = arr[i];
while(s.top() != -1 && arr[s.top()] >= curr) {
s.pop();
}
ans[i] = s.top();
s.push(i);
}
return ans;
}
public:
long long getMaxArea(long long arr[], int n) {
vector<int> next = nextSmallerElement(arr, n);
vector<int> prev = prevSmallerElement(arr, n);
long long maxArea = 0;
for(int i = 0; i < n; i++) {
long long height = arr[i];
if(next[i] == -1) {
next[i] = n;
}
long long width = next[i] - prev[i] - 1;
long long area = height * width;
maxArea = max(maxArea, area);
}
return maxArea;
}
};
// Example usage
int main() {
Solution sol;
long long arr[] = {6, 2, 5, 4, 5, 1, 6};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Maximum area: " << sol.getMaxArea(arr, n) << endl;
return 0;
}