From efe35f5321f369f64c9f5976b9ecf59ac6a2f666 Mon Sep 17 00:00:00 2001 From: DEBJIT Date: Tue, 14 Dec 2021 00:06:34 +0530 Subject: [PATCH] chore(CPlusPlus): add stock span problem (#649) Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com> --- algorithms/CPlusPlus/README.md | 1 + .../CPlusPlus/Stacks/stock-span-problem.cpp | 70 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 algorithms/CPlusPlus/Stacks/stock-span-problem.cpp diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 9c2b333c..0f8fc629 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -86,6 +86,7 @@ - [Reversing Stack](Stacks/reverse-stack.cpp) - [Stack using Array](Stacks/stack-using-array.cpp) - [Infix to postfix expression conversion](Stacks/infix-to-postfix.cpp) +- [Stock Span Problem using Stacks](Stacks/stock-span-problem.cpp) ## Sorting diff --git a/algorithms/CPlusPlus/Stacks/stock-span-problem.cpp b/algorithms/CPlusPlus/Stacks/stock-span-problem.cpp new file mode 100644 index 00000000..9ad48ebb --- /dev/null +++ b/algorithms/CPlusPlus/Stacks/stock-span-problem.cpp @@ -0,0 +1,70 @@ +/*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 +using namespace std; + +// A stack based efficient method to calculate +// stock span values +void calculateSpan(const vector price, const int n,vector& S) +{ + // Create a stack and push index of first + // element to it + stack st; + st.push(0); + + // Span value of first element is always 1 + S.push_back(1); + + // Calculate span values for rest of the elements + for (int i = 1; i < n; i++) { + // Pop elements from stack while stack is not + // empty and top of stack is smaller than + // price[i] + while (!st.empty() && price[st.top()] < price[i]) + st.pop(); + + // If stack becomes empty, then price[i] is + // greater than all elements on left of it, + // i.e., price[0], price[1], ..price[i-1]. Else + // price[i] is greater than elements after + // top of stack + int x = (st.empty()) ? (i + 1) : (i - st.top()); + S.push_back(x); + + // Push this element to stack + st.push(i); + } +} + +// A utility function to print elements of array +void printArray(const vector& arr,const int n) +{ + for (int i = 0; i < n; i++) + cout << arr[i] << " "; +} + +// Driver program to test above function +int main() +{ + vector price; + int n; + //Enter the size of price + cin>>n; + vector S; + for(int i=0;i>x; + price.push_back(x); + } + + // Fill the span values in array S[] + calculateSpan(price, n, S); + + // print the calculated span values + printArray(S, n); + + return 0; +}