πDay 4. Stock span problem π§
The problem can be found at the following link: Question Link
π‘ Problem Description:
The Stock Span Problem is a financial problem where we have a series of daily stock prices, and we need to compute the span of each day's stock price.
The span of stock price on a given day i is defined as the maximum number of consecutive days just before day i
, for which the price of the stock on the given day is less than or equal to its price on the current day.
π Example Walkthrough:
Example 1:
Input:
arr[] = [100, 80, 60, 70, 60, 75, 85]
Output:
[1, 1, 1, 2, 1, 4, 6]
Explanation:
Day 1:
100
(No previous prices) β Span =1
Day 2:
80
(Only100
before it) β Span =1
Day 3:
60
(Only100, 80
before it) β Span =1
Day 4:
70
(60
before it) β Span =2
Day 5:
60
(70, 60
before it) β Span =1
Day 6:
75
(60, 70, 60
before it) β Span =4
Day 7:
85
(75, 60, 70, 60, 80
before it) β Span =6
Example 2:
Input:
arr[] = [10, 4, 5, 90, 120, 80]
Output:
[1, 1, 2, 4, 5, 1]
Explanation:
Day 1:
10
β Span =1
Day 2:
4
β Span =1
Day 3:
5
β Span =2
Day 4:
90
β Span =4
Day 5:
120
β Span =5
Day 6:
80
β Span =1
Constraints:
$(1 \leq \text{arr.size()} \leq 10^5)$
$(1 \leq \text{arr[i]} \leq 10^5)$
π― My Approach:
Optimized Stack-Based Approach (O(N) Time, O(N) Space)
Use a stack to store indices of elements while iterating through the array.
For each price, check previous smaller elements by popping elements from the stack.
The span is the difference between current index and stack's top (or entire length if stack is empty).
Push the current index into the stack for future calculations.
Algorithm Steps:
Initialize an empty stack and a result array.
Iterate through the price array:
Pop elements from the stack while they are less than or equal to current price.
Compute span = current index - top of stack (or full length if stack is empty).
Push the current index to the stack.
Return the computed span array.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(N), since we process each element only once and each element is pushed and popped from the stack at most once.
Expected Auxiliary Space Complexity: O(N), due to the stack storing indices.
π Solution Code
Code (C++)
class Solution {
public:
vector<int> calculateSpan(vector<int>& arr) {
vector<int> span(arr.size());
stack<int> st;
for (int i = 0; i < arr.size(); i++) {
while (!st.empty() && arr[st.top()] <= arr[i]) st.pop();
span[i] = st.empty() ? i + 1 : i - st.top();
st.push(i);
}
return span;
}
};
Code (Java)
class Solution {
public ArrayList<Integer> calculateSpan(int[] arr) {
ArrayList<Integer> span = new ArrayList<>();
Stack<Integer> st = new Stack<>();
for (int i = 0; i < arr.length; i++) {
int days = 1;
while (!st.isEmpty() && arr[st.peek()] <= arr[i]) {
days += span.get(st.pop());
}
span.add(days);
st.push(i);
}
return span;
}
}
Code (Python)
class Solution:
def calculateSpan(self, arr):
span, st = [], []
for i, price in enumerate(arr):
days = 1
while st and arr[st[-1]] <= price:
days += span[st.pop()]
span.append(days)
st.append(i)
return span
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated