LeetCode 901: Online Stock Span Solution with Monotonic Stack

Day 17 / 100 Days of Code Challenge 💻🔥 Solved LeetCode 901 — Online Stock Span 📈 🔍 Problem • Design a system that calculates the stock span for each day • Span = number of consecutive days (including today) where price is less than or equal to today’s price ⚙️ Approach (Monotonic Stack) • Use a stack to store pairs of (price, span) • For each new price, remove all smaller or equal prices from the stack • Add their span to current span • Push the current price with updated span 💡 Key Learning • Strong understanding of monotonic stack pattern • Avoiding repeated comparisons using accumulated span • Optimizing from O(n²) to O(n) (amortized) ⏱ Complexity • Time: O(n) ⏳ • Space: O(n) 📦 Consistency continues 🚀 #100DaysOfCode #LeetCode #DSA #Stack #ProblemSolving #Consistency

  • graphical user interface, text, application

To view or add a comment, sign in

Explore content categories