-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock-span-problem.cpp
66 lines (55 loc) · 2.07 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
/*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 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 its price on the given day.*/
// C++ linear time solution for stock span problem
#include <bits/stdc++.h>
using namespace std;
// A stack based efficient method to calculate
// stock span values
void calculateSpan(const vector<int> price, const int priceSize,vector<int>& stockSpan)
{
stack<int> spanStack;
spanStack.push(0);
// Span value of first element is always 1
stockSpan.push_back(1);
// Calculate span values for rest of the elements
for (int priceIt = 1; priceIt < priceSize; priceIt++) {
while (!spanStack.empty() && (price[spanStack.top()] < price[priceIt]) )
spanStack.pop();
// If stack becomes empty, then price[i] is
// greater than all elements on left of it,
// i.e., price[0], price[1], ..price[priceSize-1]. Else
// price[priceSize] is greater than elements after
// top of stack
stockSpan.push_back( (spanStack.empty()) ? (priceIt + 1) : (priceIt - spanStack.top()) );
spanStack.push(priceIt);
}
}
//Print elements of array
void printArray(const vector<int>& arr,const int arrSize)
{
for (int arrIt = 0; arrIt < arrSize; arrIt++)
cout << arr[arrIt] << " ";
}
//Return the price vector from user input
vector<int> getPrice()
{
vector<int> price;
int priceSize;
cin>>priceSize;
for(int sizeIt = 0 ; sizeIt < priceSize ; sizeIt++)
{
int priceEntry;
cin>>priceEntry;
price.push_back(priceEntry);
}
return price;
}
// Driver program to test above function
int main()
{
vector<int> price = getPrice();
vector<int> stockSpan;
calculateSpan(price, price.size(), stockSpan);
printArray(stockSpan, price.size());
return 0;
}