-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacks.cpp
80 lines (60 loc) · 1.77 KB
/
stacks.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
/* Given an integer array arr[] of size N, find the maximum of the minimums for every window size in the given array.
I have solved this program using stack */
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
vector<int> findNextSmaller(vector<int>& arr, int n) {
vector<int> result(n, -1);
stack<int> s;
for (int i = 0; i < n; i++) {
while (!s.empty() && arr[s.top()] > arr[i]) {
result[s.top()] = i;
s.pop();
}
s.push(i);
}
return result;
}
vector<int> findPrevSmaller(vector<int>& arr, int n) {
vector<int> result(n, n);
stack<int> s;
for (int i = n - 1; i >= 0; i--) {
while (!s.empty() && arr[s.top()] > arr[i]) {
result[s.top()] = i;
s.pop();
}
s.push(i);
}
return result;
}
int maxOfMinForEveryWindowSize(vector<int>& arr, int n) {
vector<int> nextSmaller = findNextSmaller(arr, n);
vector<int> prevSmaller = findPrevSmaller(arr, n);
vector<int> windowSize(n, 0);
for (int i = 0; i < n; i++) {
int window = nextSmaller[i] - prevSmaller[i] - 1;
windowSize[window] = max(windowSize[window], arr[i]);
}
for (int i = n - 2; i >= 0; i--) {
windowSize[i] = max(windowSize[i], windowSize[i + 1]);
}
int result = 0;
for (int i = 0; i < n; i++) {
result += windowSize[i];
}
return result;
}
int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;
vector<int> arr(n);
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int answer = maxOfMinForEveryWindowSize(arr, n);
cout << "Maximum of the minimums for every window size: " << answer << endl;
return 0;
}