forked from anuraggarg3/anuraggarg3.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStock Span Problem.cpp
50 lines (46 loc) · 951 Bytes
/
Stock Span Problem.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
#include <bits/stdc++.h>
using namespace std;
int main()
{
stack<pair<int,int>>s;
vector<int>v;
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{
if(s.size()==0)
{
v.push_back(-1);
}
else if(s.size()>0 && s.top().first>a[i])
{
v.push_back(s.top().second);
}
else if(s.size()>0 && s.top().first<=a[i])
{
while(!s.size()==0 && s.top().first<=a[i])
{
s.pop();
}
if(s.size()==0)
{
v.push_back(-1);
}
else
{
v.push_back(s.top().second);
}
}
s.push({a[i],i});
}
for(int i=0;i<v.size();i++)
{
cout<<i-v[i]<<" ";
}
return 0;
}