Stock Span ProblemIn this tutorial, we will write the Python program for the stock span problem. It is quite popular programming problem that is frequently asked in technical interviews. The stock span problem is a financial challenge that involves analyzing a series of N daily price quotes for a particular stock. The objective is to determine the span of the stock's price for each of the N days. The span Si for a given day i represent the maximum number of consecutive days immediately preceding the current day, where the stock's price was lower than or equal to its price on the given day. Example - Input: N = 7, price = [100 80 60 70 60 75 85] Output: 1 1 1 2 1 4 6 Explanation: Traversing the given input span for 100 will be 1, 80 is smaller than 100 so the span is 1, 60 is smaller than 80 so the span is 1, 70 is greater than 60 so the span is 2 and so on. Hence the output will be 1 1 1 2 1 4 6. Input: N = 6, price = [10 4 5 90 120 80] Output: 1 1 2 4 5 1 Explanation: Traversing the given input span for 10 will be 1, 4 is smaller than 10 so the span will be 1, 5 is greater than 4 so the span will be 2 and so on. Hence, the output will be 1 1 2 4 5 1. To solve this problem, we will use the Naïve approach Solution -First, we use the naïve approach. To solve the stock span problem, we can traverse the input price array. For each element being visited, we iterate through the elements on its left side and increment the span value of the current element as long as the elements on the left side are smaller than or equal to it. Example - Output: [1, 1, 2, 4, 5, 1] Explanation - In the above code, we define the getSpan() function that calculates the span values for a given price array.
The time complexity of the above code is O(n2) and auxiliary space is O(n). Method - 2 Using StackIn this approach, first we create an empty stack int type and push 0 in it. Now, we set the default value 1 to day 1 and iterate the rest of the days. While the stack is not empty and the price of st.top is less than or equal to the price of the current day, pop out the top value. Now, assign the value of the current day as i+1 if stack is empty else equal to i - st.top. Push the current day into the stack and print the result. Let's understand the following example. Note - We will not pop from the stack when the current and previous stock prices are sameExample - Output: [1, 1, 2, 4, 5, 1, 0] Explanation - In the above code, we define the getSpan() function takes two arguments: price (a list of stock prices) and S (not used in the code and should be removed).
The time complexity of the above approach is O(N) and the auxiliary space is O(N). Method -3 Using Dynamic ProgrammingIn this approach, we will store the value of every index and calculate the value of the next index using the previous value. Now, we will check the value of the previous element and if the value of the current element is greater than the previous element. We will add the value of the previous into the current index and check the value of (previous index -value of previous index) and check the condition repeatedly. Let's understand the following example. Example - Output: [1, 1, 2, 4, 5, 1, 0] Explanation - In the above code, we create the getSpan() function that takes two parameters: price (a list of stock prices) and S (initially an empty list, but will be used to store the spans).
Method - 4: Using two StacksIn this approach, we will use the two stacks where one stack stores the actual stock prices whereas; the other stack is a temporary stack. Let's understand the following example. Example - Output: [1, 1, 2, 4, 5, 1] The time complexity of this approach is O(N) and auxiliary space is O(N). ConclusionIn this tutorial, we have discussed various approaches to solving the stock span problem. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India