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
LeetCode 901: Online Stock Span Solution with Monotonic Stack
More Relevant Posts
-
🚀 #100DaysOfCode Day 77/100 – Permutations 🧠 Problem: Given an array of distinct integers, return all possible permutations. 👉 Order matters here → [1,2,3] ≠ [3,2,1] 💡 Core Idea This is a classic Backtracking + Swapping problem 🔥 1️⃣ Fix one element at current index 2️⃣ Swap it with every possible element 3️⃣ Recursively generate permutations for remaining 4️⃣ Backtrack (swap back) 👉 Swap → Recurse → Undo 📚 Key Learnings 1. Backtracking with swapping technique 2. Generating all permutations efficiently 3. Understanding recursion tree deeply ⏱️ Complexity Time Complexity: O(n!) Space Complexity: O(n) (recursion stack) #100DaysOfCode #Day77 #LeetCode #DSA #Backtracking #Recursion #Algorithms #CodingJourney #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 7 🚀 Solved: Balanced Binary Tree (LeetCode 110) This problem builds on recursion and tree depth. A tree is balanced if the height difference between left and right subtrees is at most 1 for every node. 💡 My approaches: 🔹 Approach 1 (Brute Force): For every node, calculate left and right subtree heights Check the difference and recurse further 🔹 Approach 2 (Optimal – Postorder DFS): Compute height and balance together in one traversal Avoid recomputing subtree heights 💡 Key learning: Instead of solving subproblems separately, combine them in a single DFS to optimize performance. ⏱️ Time Complexity: Brute Force → O(n²) Optimal → O(n) 🔗 GitHub: https://lnkd.in/gVdJeg2a #DSA #LeetCode #Coding
To view or add a comment, sign in
-
Day 31/75 🚀 Solved Maximum Twin Sum of a Linked List (LeetCode 2130) today! ✅ All 46/46 test cases passed ⚡ Runtime: 3 ms (Beats ~67%) 💾 Memory: 124.34 MB (Beats ~67%) 🔍 Approach: Converted the linked list into an array for easy access. ✔️ Stored all node values in a vector ✔️ Used two pointers → one at start, one at end ✔️ Calculated twin sum (list[i] + list[n-1-i]) ✔️ Tracked maximum among all pairs This avoids complex pointer manipulation. 💡 Key Learning: Sometimes extra space simplifies logic a lot. Choosing clarity over optimization can be a smart move. Consistency + smart decisions = efficient solutions 💡 #LeetCode #CPP #DSA #ProblemSolving #CodingJourney #75DaysOfCode #Focused
To view or add a comment, sign in
-
-
Days 53 & 54/120: Word break - backtracking meets memoization One problem across two days. A hybrid approach. Word Break Problem (Backtracking) Given a string and a dictionary, split the string into space-separated words from the dictionary. Return all possible ways. Pure backtracking approach: for each position, try every possible word that matches the prefix. Recursively solve the remaining string. Combine results. The optimization: memoization. Without it, we recompute the same substring splits repeatedly. With a DP map storing results for each substring, we avoid redundant work. Why two days: Day 1 was getting the backtracking logic right - splitting at each position, checking dictionary, building results. Day 2 was adding memoization. The first solution worked but was exponentially slow for longer strings. Caching transformed it from timing out to instant. The pattern: This combines two techniques: backtracking for exploring all possibilities, DP for avoiding repeated computation. Neither alone is enough - backtracking without memoization is too slow, DP without recursion doesn't generate all solutions. Fifty-four days in. Learning when to combine techniques, not just use them individually. #DSA #CodingJourney #100DaysOfCode #LeetCode #GeeksForGeeks #Backtracking #DynamicProgramming #Memoization #Algorithms #SoftwareEngineering #Programming #Learning #Day53 #Day54
To view or add a comment, sign in
-
-
🚀 Day 70 of #100DaysOfCode 📌 LeetCode Q3: 3Sum 💡 Problem: Find all unique triplets in the array which gives the sum of 0. 🧠 Approach I Used: - First, sorted the array - Fixed one element - Applied two-pointer technique for the remaining part - Skipped duplicates to avoid repeated triplets ⚡ Key Insight: Instead of brute force O(n³), using sorting + two pointers reduces it to O(n²) ❗ Edge Cases Handled: - Duplicate values - No valid triplets - Negative + positive mix ⏱ Complexity: - Time: O(n²) - Space: O(1) (excluding result) 🔥 Takeaway: Two-pointer + sorting = deadly combo for array problems 💯 💬 Open to feedback & better approaches! #Day70 #100DaysOfCode #LeetCode #DSA #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 90 of #100DaysOfCode Today I solved "Path Sum III" on LeetCode using a DFS + Recursion approach. Key Idea: We need to count all paths where the sum of node values equals the target. The path doesn’t have to start from the root — it can start from any node! Approach: • For every node, treat it as a starting point • Use DFS to explore all downward paths • Reduce the target at each step (target - node->val) • If a node matches the remaining target → count it • Repeat the process for left and right subtrees Why this works: Every node gets a chance to act as the starting point, and DFS ensures we explore all possible paths efficiently. Concepts Used: • Binary Trees • Depth First Search (DFS) • Recursion • Backtracking (implicit) Time Complexity: O(n²) in worst case Space Complexity: O(h) This problem helped me understand how to explore all possible paths in a tree, not just root-based ones — a big step forward in mastering tree problems From single path problems → to handling multiple dynamic paths… growing every day #Day90 #100DaysOfCode #LeetCode #BinaryTree #DFS #Cpp #CodingJourney
To view or add a comment, sign in
-
-
Day 12/30 – LeetCode Challenge Today’s problem: Minimum Distance Between Equal Elements (3740) Problem Overview We are given: - an array "nums" We need to: - find the minimum value of: 2 * (k - i) such that: - nums[i] = nums[j] = nums[k] - i < j < k If no such triplet exists, return -1. Approach I used a single optimized approach based on grouping indices: - store indices of each element using a map - for each unique number, we get all positions where it appears Then: - if the frequency is less than 3 → skip - otherwise, iterate through the index list and check consecutive triplets: - take (v[j], v[j+1], v[j+2]) - compute: 2 * (v[j+2] - v[j]) - keep updating the minimum answer Complexity - Time Complexity: O(n) - Space Complexity: O(n) Key Learning - derive a direct formula from index operations instead of brute force - be careful with loop boundaries even in simple logic YouTube Link: https://lnkd.in/gRnc3drP I’ve tried explaining this code. Would love to hear your valuable feedback! #LeetCode #Day12 #DSA #ProblemSolving #CPP #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 11 of Coding Solved Problem #1047 – Remove All Adjacent Duplicates in String 💡 🔍 Problem Statement: Given a string, repeatedly remove adjacent duplicate characters until no duplicates remain. Return the final string. 🧠 Key Learning: Used Stack Data Structure to efficiently track characters Learned how to simulate removal of adjacent duplicates Understood how LIFO helps in matching recent characters ⚡ Approach: Traverse the string If current character == top of stack → pop Else → push into stack Convert stack back to string for final answer ⏱️ Complexity: Time: O(n) Space: O(n) 💻 Tech: C++ | DSA Consistency is the key 🔥 #Day11 #CodingJourney #DSA #LeetCode #Cpp #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 37/75 Solved Path Sum III (LeetCode 437) today! 🌳 ✅ All 130/130 test cases passed ⚡ Runtime: 7 ms (Beats ~59%) 💾 Memory: 18.94 MB (Beats ~76%) 🔍 Approach: Used DFS (recursion) to explore all possible paths in the binary tree. ✔️ For each node, calculated cumulative sum along the path ✔️ Checked if the current sum matches the target ✔️ Started fresh DFS from every node to cover all paths ✔️ Counted all valid paths where sum equals target This ensures we consider every possible path efficiently. 💡 Key Learning: Tree problems often require exploring every node as a starting point. Combining recursion with careful state tracking makes solutions more effective. Consistency + practice = better problem solving 🚀 #LeetCode #CPP #DSA #ProblemSolving #CodingJourney #75DaysOfCode #Focused
To view or add a comment, sign in
-
-
Day 26/75 🚀 Solved Decode String (LeetCode 394) today! ✅ All 34/34 test cases passed ⚡ Runtime: 0 ms (Beats 100%) 💾 Memory: 8.22 MB (Beats ~99%) 🔍 Approach: Used a stack-based approach to decode the string. ✔️ Traverse the string character by character ✔️ If character is not ‘]’ → push into result ✔️ When ‘]’ is found: • Extract substring inside brackets • Get the number (repeat count) before '[' • Repeat the substring and append back This continues until the entire string is decoded. 💡 Key Learning: Whenever you see nested patterns (like brackets) → think of stack. It helps manage order and structure efficiently. Consistency + patience = clean solutions 💯 #LeetCode #CPP #DSA #ProblemSolving #CodingJourney #75DaysOfCode #Focused
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development