-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_span_problem.cpp
More file actions
37 lines (31 loc) · 1 KB
/
Stack_span_problem.cpp
File metadata and controls
37 lines (31 loc) · 1 KB
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
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v;
stack<pair<int, int>> s;
int c[] = {100, 80, 60, 70, 60, 75, 85};
int n = sizeof(c) / sizeof(c[0]); // Calculate the size of array c
for (int i = 0; i < n; i++) {
if (s.size() == 0) {
v.push_back(-1);
} else if (s.size() > 0 && s.top().first > c[i]) { //change is just "".first"
v.push_back(s.top().second);
} else if (s.size() > 0 && s.top().first <= c[i]) { //change is just "".first"
while (s.size() > 0 && s.top().first <= c[i]) { //change is just "".first"
s.pop();
}
if (s.size() == 0)
v.push_back(-1);
else
v.push_back(s.top().second);
}
s.push(make_pair(c[i], i));
}
reverse(v.begin(), v.end());
for (int i = v.size() - 1; i >= 0; i--) {
cout << v[i] << " ";
}
cout << endl;
return 0;
}