githubEdit

21. Stock Span Problem

βœ… GFG solution to the Stock Span Problem: calculate consecutive days with stock prices less than or equal to current day using stack-based approach. πŸš€

The problem can be found at the following link: πŸ”— Question Linkarrow-up-right

🧩 Problem Description

The stock span problem is a financial problem where we have a series of daily price quotes for a stock and we need to calculate the span of stock price for all days.

You are given an array arr[] representing daily stock prices. The stock span for the i-th day is the number of consecutive days up to day i (including day i itself) for which the price of the stock is less than or equal to the price on day i. Return the span of stock prices for each day in the given sequence.

πŸ“˜ Examples

Example 1

Input: arr[] = [100, 80, 90, 120]
Output: [1, 1, 2, 4]
Explanation: Traversing the given input span 100 is greater than equal to 100 and there are no more days behind it so the span is 1, 80 is greater than equal to 80 and smaller than 100 so the span is 1, 90 is greater than equal to 90 and 80 so the span is 2, 120 is greater than 90, 80 and 100 so the span is 4. So the output will be [1, 1, 2, 4].

Example 2

Input: arr[] = [10, 4, 5, 90, 120, 80]
Output: [1, 1, 2, 4, 5, 1]
Explanation: Traversing the given input span 10 is greater than equal to 10 and there are no more days behind it so the span is 1, 4 is greater than equal to 4 and smaller than 10 so the span is 1, 5 is greater than equal to 4 and 5 and smaller than 10 so the span is 2, and so on. Hence the output will be [1, 1, 2, 4, 5, 1].

πŸ”’ Constraints

  • $1 \le \text{arr.size()} \le 10^5$

  • $1 \le \text{arr}[i] \le 10^5$

βœ… My Approach

The optimal approach uses a Monotonic Stack technique to efficiently calculate the stock span in linear time:

Stack-Based Approach

  1. Initialize Data Structures:

    • Create a result vector to store span values for each day.

    • Use a stack to store indices of days (not prices directly).

  2. Process Each Day:

    • For the current day i, pop elements from the stack while the stack is not empty and the price at stack's top index is less than or equal to current day's price.

    • This removes all consecutive previous days with lower or equal prices.

  3. Calculate Span:

    • If stack becomes empty after popping, it means all previous days had lower or equal prices, so span = i + 1.

    • Otherwise, span = current index - index at stack's top (the last day with a higher price).

  4. Push Current Index:

    • Push the current day's index onto the stack for future comparisons.

  5. Repeat:

    • Continue this process for all days to build the complete span array.

πŸ“ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n), where n is the size of the array. Each element is pushed and popped from the stack at most once, resulting in a total of 2n operations in the worst case, which simplifies to O(n).

  • Expected Auxiliary Space Complexity: O(n), as we use a stack to store indices. In the worst case (when prices are in increasing order), the stack will contain all n indices.

πŸ§‘β€πŸ’» Code (C++)

chevron-right⚑ View Alternative Approaches with Code and Analysishashtag

πŸ“Š 2️⃣ Two-Pointer Backward Scan

πŸ’‘ Algorithm Steps:

  1. For each day, initialize span as 1 (the current day itself).

  2. Use a pointer to scan backwards from the previous day.

  3. Jump to the previous greater element using previously computed spans.

  4. Continue until finding a day with a higher price or reaching the start.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n) - Each element processed once with efficient jumps

  • Auxiliary Space: πŸ’Ύ O(1) - Only result array, no extra structures

βœ… Why This Approach?

  • No stack overhead for memory management

  • Efficient jumping using previously computed spans

  • Good cache locality with backward traversal

πŸ“Š 3️⃣ Brute Force Nested Loop

πŸ’‘ Algorithm Steps:

  1. For each day, start counting consecutive days backward.

  2. Continue counting while the previous day's price is less than or equal to current.

  3. Stop when a higher price is found or array start is reached.

  4. Store the count as span for current day.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(nΒ²) - Nested loops checking all previous elements

  • Auxiliary Space: πŸ’Ύ O(1) - Constant extra space

βœ… Why This Approach?

  • Simplest logic with straightforward implementation

  • No additional data structures needed

  • Best for very small input sizes

Note: This approach results in Time Limit Exceeded (TLE) for large inputs (fails ~1110/1115 test cases due to time constraints).

πŸ“Š 4️⃣ Dynamic Programming with Memoization

πŸ’‘ Algorithm Steps:

  1. Store the index of the nearest greater element for each position.

  2. Use memoization to avoid recalculating spans.

  3. For each element, use the nearest greater index to compute span.

  4. Build solution incrementally using previous results.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n) - Linear traversal with memoized jumps

  • Auxiliary Space: πŸ’Ύ O(n) - Extra array for nearest greater element

βœ… Why This Approach?

  • Efficient jumps using memoization

  • Clear separation of concerns with NGE array

  • Useful for problems requiring nearest greater element

πŸ†š πŸ” Comparison of Approaches

πŸš€ Approach

⏱️ Time Complexity

πŸ’Ύ Space Complexity

βœ… Pros

⚠️ Cons

🏷️ Stack-Based

🟒 O(n)

🟑 O(n)

πŸš€ Optimal time, clean code

πŸ’Ύ Stack overhead

πŸ” Two-Pointer Scan

🟒 O(n)

🟒 O(1)

πŸ“– Space efficient

πŸ”§ Complex jump logic

πŸ“Š Brute Force

πŸ”΄ O(nΒ²)

🟒 O(1)

🎯 Simple to understand

🐌 Slow for large inputs

πŸ”„ DP Memoization

🟒 O(n)

🟑 O(n)

⭐ Educational approach

πŸ’Ύ Extra space for NGE array

πŸ† Best Choice Recommendation

🎯 Scenario

πŸŽ–οΈ Recommended Approach

πŸ”₯ Performance Rating

πŸ… Overall best performance

πŸ₯‡ Stack-Based

β˜…β˜…β˜…β˜…β˜…

πŸ“– Memory constrained

πŸ₯ˆ Two-Pointer Scan

β˜…β˜…β˜…β˜…β˜†

πŸ”§ Small input size

πŸ₯‰ Brute Force

β˜…β˜…β˜…β˜†β˜†

🎯 Learning/Educational

πŸ… DP Memoization

β˜…β˜…β˜…β˜…β˜†

β˜• Code (Java)

🐍 Code (Python)

🧠 Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: πŸ“¬ Any Questions?arrow-up-right. Let's make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


πŸ“Visitor Count

Visitor counter

Last updated