How to Solve Stock Span Problem Using Python
In this tutorial, we will solve the stock span problem using the Python code. Before proceeding to the solution, let's understand the problem statement of this problem.
What is Stock Span Problem?
The stock span problem is financial problem where we calculate the share price. In this problem, we have a series of n daily price quotes and we need to calculate the span of stock's price for all n days.
Suppose 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].
Code Implementation -
The following solution is simple but inefficient where we traverse the input price array. When we visit the every element, traverse elements on the left of it and increment the span value of it while elements on the left side are smaller.
1 1 2 4 5 1
The above method takes the 0(n2) to solve the problem. Let's try to reduce the time complexity by the 0(n) times.
A Linear-Time Complexity Method
We can make the above solution efficient using linear-time complexity method. In this method, S[i] where i can be calculated if we know the closed day preceding i, such that the price is greater than on that day than the price on the day i. If such condition occurs, let's call it h(i), otherwise, we define h(i) =-1.
We will implement this logic using the stack data structure.
1 1 2 4 5 1
In the above code, we defined the a getSpan() method that takes the lists as arguments. First we calculated the length of the price list and initialized the empty stack. We fixed the span value of the first element as 1. Then, we visited each element using for loop.
In for loop iteration, we iterated while loop until pop elements from stack while stack is not empty and top of stack is smaller than price[i]. Then we checked, if the stack becomes empty, price[i] is greater than all elements on left of it, i.e. price, price……price[i-1], otherwise the price[i] is greater than elements after top of stack. If the condition is true pushed element to the stack.
We created a utility function to print elements of array. At the end, we initialized price and span list. Now called the getSpan() function along with the printList(). It printed the span values.
The time complexity of this method will be 0(n) which is better than the previous one. We can see that every element of the array is added and removed from the stack at most once.
Let's see another approach.
Without Using Stack
1 1 2 4 5 1
In this approach, we created a getSpan() function that takes three arguments - List, length, and result. We fixed the first element as 1 and calculate the rest span values of the element. We defined the utility function to print the elements of the list. At the end, we called the getSpan() method and it returned the span values.
This problem commonly asked in the technical interview and you can find this problem top coding practice sites like leetcode.com. In this tutorial, we have implemented the solution of span problem using the three approaches. First solution is quite simple but inefficient in terms of memory.
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected]
Duration: 1 week to 2 week