-
Notifications
You must be signed in to change notification settings - Fork 2
/
stock_span_problem.cpp
73 lines (54 loc) · 1.89 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
The stock span problem is a financial problem where we have a series of n daily price quotes for a stock and we need to calculate the span of stock’s price for all n days.
The span Si of the stock’s price on a given day i is defined as the maximum number of consecutive days just before the given day, for which the price of the stock on the current day is less than or equal to its price on the given day.
For example, if an array of 7 days prices is given as {100, 80, 60, 70, 60, 75, 85}, then the span values for corresponding 7 days are {1, 1, 1, 2, 1, 4, 6}.
Input:
The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N, N is the size of the array. The second line of each test case contains N input C[i].
Output:
For each testcase, print the span values for all days.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 200
1 ≤ C[i] ≤ 800
Example:
Input:
2
7
100 80 60 70 60 75 85
6
10 4 5 90 120 80
Output:
1 1 1 2 1 4 6
1 1 2 4 5 1
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++) cin>>a[i];
stack<pair<int, int> >s;
vector<int> v;
for(int i=0;i<n;i++){
if(s.empty()) v.push_back(-1);
else if(!s.empty() && s.top().first > a[i]) v.push_back(s.top().second);
else if(!s.empty() && s.top().first<=a[i]){
while(!s.empty() && s.top().first<=a[i]){
s.pop();
if(s.empty()) v.push_back(-1);
else if(!s.empty() && s.top().first > a[i]) v.push_back(s.top().second);
}
}
s.push({a[i], i});
}
for(int i=0;i<n;i++) cout<<i-v[i]<<" ";
cout<<endl;
}
return 0;
}