-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax-area-histogram.cpp
109 lines (78 loc) · 2.47 KB
/
max-area-histogram.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//Maximum area histogram
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
//Next smaller element
vector<int> computeNSE(int arr[], int size);
//Previous smaller element
vector<int> computePSE(int arr[], int size);
void displayVector(vector<int>, int);
int calculateMaximumArea(int *, vector<int>, vector<int>, int);
int main(){
int N;
cout <<"Enter the size of array :"; cin>>N;
int arr[N];
for(int i = 0; i < N; i++) cin >>arr[i];
cout << "Computing NSE\n";
vector<int> right = computeNSE(arr, N);
cout << "NSE vector stored in right vector.\n";
cout << "Computing PSE\n";
vector<int> left = computePSE(arr, N);
cout << "PSE vector stored in left vector.\n";
displayVector(right, N);
displayVector(left, N);
int result = calculateMaximumArea(arr, right, left, N);
cout << "Max area : " << result <<endl;
return 0;
}
vector<int> computeNSE(int arr[], int size){
vector<int> v;
stack< pair<int,int> > s;
cout << "NSE computation started...\n";
for(int i = size-1; i >= 0; i--){
if(s.empty()) v.push_back(size);
else if(s.top().first < arr[i]) v.push_back(s.top().second);
else{
while(!s.empty() && s.top().first >= arr[i]) s.pop();
v.push_back(s.empty() ? size : s.top().second);
}
s.push({arr[i], i});
}
cout << "NSE computed (rev)\n";
vector<int> rev;
for(int i = size-1; i>=0 ; i--){
rev.push_back(v[i]);
}
cout << "NSE computed (non-rev) returning vector...\n";
return rev;
}
vector<int> computePSE(int arr[], int size){
vector<int> v;
stack< pair<int, int> > s;
cout << "PSE computation started...\n";
for(int i = 0; i < size; i++){
if(s.empty()) v.push_back(-1);
else if(s.top().first < arr[i]) v.push_back(s.top().second);
else{
while(!s.empty() && s.top().first >= arr[i]) s.pop();
v.push_back(s.empty() ? -1 : s.top().second);
}
s.push({arr[i], i});
}
cout << "PSE computation finished, returning vector...\n";
return v;
}
void displayVector(vector<int> v, int size){
for(int i = 0; i < size; i++){
cout << v[i] << " ";
}
cout << endl;
}
int calculateMaximumArea(int *arr, vector<int> right, vector<int> left, int size){
int max = 0;
for(int i = 0; i < size; i++){
int res = (right[i] - left[i] - 1) * arr[i];
max = res > max ? res : max;
}
return max;
}