forked from TARANG0503/DSA-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstockspan.cpp
More file actions
46 lines (39 loc) · 1.32 KB
/
Copy pathstockspan.cpp
File metadata and controls
46 lines (39 loc) · 1.32 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
38
39
40
41
42
43
44
45
46
/*
TARANG0503 has been working with an organization called 'Money Traders' for the past few years. The organization is into the money trading business. His manager assigned him a task. For a given array/list of stock's prices for N days, find the stock's span for each day.
The span of the stock's price today is defined as the maximum number of consecutive days(starting from today and going backwards) for which the price of the stock was less than today's price.
For example, if the price of a stock over a period of 7 days are [100, 80, 60, 70, 60, 75, 85], then the stock spans will be [1, 1, 1, 2, 1, 4, 6].
*/
#include <iostream>
using namespace std;
#include<stack>
int* stockSpan(int *price, int size) {
int *a=new int[size];
int i=0;
stack<int>s;
s.push(0);
a[0]=1;
for(i=1;i<size;i++)
{
while (!s.empty() && price[s.top()] <price[i]) {
s.pop();
}
a[i] = (s.empty()) ? (i + 1) : (i - s.top());
s.push(i);
}
return a;
}
int main() {
int size;
cin >> size;
int *input = new int[size];
for (int i = 0; i < size; i++) {
cin >> input[i];
}
int *output = stockSpan(input, size);
for (int i = 0; i < size; i++) {
cout << output[i] << " ";
}
cout << "\n";
delete[] input;
delete[] output;
}