Day 44 :-𝗘𝗳𝗳𝗶𝗰𝗶𝗲𝗻𝗰𝘆 𝗶𝗻 𝗠𝗼𝘁𝗶𝗼𝗻: 𝗦𝗹𝗶𝗱𝗶𝗻𝗴 𝗪𝗶𝗻𝗱𝗼𝘄 𝗠𝗮𝘅𝗶𝗺𝘂𝗺 ⚡ Today’s DSA session was all about thinking beyond brute force. I worked on the Sliding Window Maximum problem on LeetCode — a classic that initially pushes you toward an O(n * k) approach, but rewards you heavily when you discover the right pattern. By applying the Monotonic Deque technique, I transformed the solution into an efficient linear-time algorithm. 🔹 𝗧𝗵𝗲 𝗧𝗲𝗰𝗵𝗻𝗶𝗰𝗮𝗹 𝗛𝗶𝗴𝗵𝗹𝗶𝗴𝗵𝘁𝘀 🔸 Monotonic Deque Strategy Instead of recalculating the maximum for every window, I maintained a deque of indices in decreasing order of their values. 👉 This ensures the maximum element is always at the front. 🔸 Smart Pruning • Out-of-Window Removal: Indices that fall outside the current window are removed from the front. • Maintaining Order: Before inserting a new element, all smaller elements at the back are removed. 👉 Because they can never become the maximum in future windows. 🔸 Constant-Time Maximum Access With this structure, the maximum of each window is always available at peekFirst() → O(1) access. 🔹 𝗘𝗳𝗳𝗶𝗰𝗶𝗲𝗻𝗰𝘆 ⚡ • Time Complexity → O(n) • Space Complexity → O(k) Each element is processed at most twice (added + removed), making the solution clean and optimal. 🔹 𝗞𝗲𝘆 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆 💡 👉 Optimization isn’t about doing more work faster — it’s about avoiding unnecessary work entirely. 👉 Choosing the right data structure (Deque here) can completely change the game. 🙏 Huge thanks to my mentors Anchal Sharma and Ikshit .. for their constant guidance and support. Their insights are helping me focus on patterns over brute force and stay consistent on this journey. #DSA #Java #100DaysOfCode #SlidingWindow #Deque #MonotonicQueue #ProblemSolving #Mentorship #LeetCode #SoftwareEngineering #CodingJourney 🚀
Efficient Sliding Window Maximum with Monotonic Deque
More Relevant Posts
-
🚀 LeetCode Biweekly Contest 180 — My Experience ━━━━━━━━━━━━━━━━━━━━━━━━━━━ 🔥 Solved 3 Problems Today ━━━━━━━━━━━━━━━━━━━━━━━━━━━ 🟢 Problem 1: Traffic Signal Logic Very straightforward problem. 👉 Just needed to check conditions on timer: 0 → Green 30 → Orange 30 < timer ≤ 90 → Red Else → Invalid 💡 No heavy logic — purely condition-based problem. 🟡 Problem 2: Count Digit Occurrences Given an array and a digit (0–9), we need to count how many times that digit appears across all numbers. 👉 Approach: Traverse each number Extract digits using % 10 Compare with given digit Maintain count 💡 Simple but tests your number handling + basics clarity 🔴 Problem 3: Minimum Operations to Make Alternating Prime (Medium) This was the most interesting one 🔥 👉 Requirement: Even index → Prime number Odd index → Non-prime number Operation → Increment by 1 🧠 My Approach: ✔️ For even index: If not prime → find nearest next prime Add difference to operations ✔️ For odd index: If prime → find nearest non-prime Add difference to operations ⚡ Key Learnings: ✅ Prime number handling is very important ✅ Edge cases (1, 2, small numbers) matter a lot ✅ Always think in terms of minimum operations ✅ Medium problems are mostly about pattern + clarity, not complexity 📈 Final Takeaway: Not every contest is about solving all problems, it’s about improving your thinking speed & accuracy. Today: ✔️ Strengthened fundamentals ✔️ Practiced implementation ✔️ Learned optimization mindset 💬 How many problems did you solve in this contest? Let’s discuss approaches 👇 #LeetCode #DSA #Coding #ProblemSolving #Java #Consistency
To view or add a comment, sign in
-
🔥 Day 31/100 – DSA Problem Solving 📌 Problem: 412. Fizz Buzz (LeetCode) Today’s problem looks simple, but it really tests your logic building and attention to detail. 💡 My Approach: I used a loop from 1 to n For each number: If divisible by both 3 & 5 → "FizzBuzz" If divisible by 3 → "Fizz" If divisible by 5 → "Buzz" Otherwise → convert number to string 🧠 What I Learned Today: ✅ Importance of using the correct variable inside loops ✅ Order of conditions matters (3 & 5 first) ✅ Writing clean and readable logic ✅ Even easy problems can teach debugging skills 🚀 Key Takeaway: Don’t underestimate easy problems — they help build strong fundamentals and improve problem-solving thinking. 💻 Code Concept: Used simple loop + conditional statements + list to store results. #Day31 #100DaysOfCode #DSA #LeetCode #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 74 — Slow & Fast Pointer (Cycle Detection in Linked Lists) Continuing the slow‑fast pointer pattern — today I solved the two foundational problems that every DSA learner encounters. No guilt about past breaks, just consistent action. 📌 Problems Solved Today: - LeetCode 141 – Linked List Cycle - LeetCode 142 – Linked List Cycle II 🧠 Key Learnings: 1️⃣ Detecting a Cycle (LeetCode 141) - Initialize `slow` and `fast` at `head`. - `slow` moves 1 step, `fast` moves 2 steps. - If they meet → cycle exists. - If `fast` reaches `null` → no cycle. - Edge case: empty list or single node → return false immediately. 2️⃣ Finding the Cycle Start (LeetCode 142) - First detect cycle using same two pointers. - Once they meet, reset `slow` to `head`. - Move both pointers 1 step each until they meet again. - The meeting point is the start of the cycle. 3️⃣ The Math Behind It (Why this works) Let distance from head to cycle start = X, cycle start to meeting point = Y, meeting point back to cycle start = Z. - Slow travels: `X + Y` - Fast travels: `X + Y + Z + Y` (because it lapped the cycle) - Fast speed = 2 × Slow speed → `2(X + Y) = X + 2Y + Z` → simplifies to `X = Z`. - So resetting slow to head and moving both 1 step makes them meet exactly at cycle start. 💡 Takeaway: These two problems are the gateway to understanding cycle‑related linked list problems. Master this pattern, and you unlock ~40% of linked list interview questions. No looking back — just solving, learning, and moving forward. #DSA #SlowFastPointer #FloydCycleDetection #LinkedList #LeetCode #CodingJourney #Revision #Java #ProblemSolving #Consistency #GrowthMindset #TechCommunity #LearningInPublic
To view or add a comment, sign in
-
🚀 Day 29/60 — LeetCode Discipline Problem Solved: Longest Common Prefix Difficulty: Easy Today’s problem focused on identifying the longest common starting pattern across multiple strings — a classic exercise in string comparison and iteration. The approach involved checking characters position by position across all strings until a mismatch is found. This reinforces how simple iterative logic can elegantly solve problems involving multiple inputs. It’s a reminder that sometimes, clarity in approach matters more than complexity in code. 💡 Focus Areas: • Strengthened string traversal techniques • Practiced comparing multiple inputs efficiently • Improved understanding of edge cases (empty strings, mismatch handling) • Reinforced early stopping conditions for optimization • Focused on clean and readable logic ⚡ Performance Highlight: Achieved efficient runtime with minimal overhead. Finding common ground across multiple inputs is not just a coding skill — it’s a pattern recognition mindset. #LeetCode #60DaysOfCode #100DaysOfCode #DSA #Strings #Algorithms #ProblemSolving #CodingJourney #SoftwareEngineering #Java #Developers #Consistency #TechGrowth #LearnToCode
To view or add a comment, sign in
-
-
Day 46 :- 𝗗𝗲𝗽𝘁𝗵 𝗠𝗮𝘁𝘁𝗲𝗿𝘀: 𝗠𝗮𝘅𝗶𝗺𝘂𝗺 𝗗𝗲𝗽𝘁𝗵 𝗼𝗳 𝗕𝗶𝗻𝗮𝗿𝘆 𝗧𝗿𝗲𝗲 🌳 Today’s DSA session was focused on mastering tree traversal fundamentals with LeetCode 104: Maximum Depth of Binary Tree. At first glance, it’s a simple problem — but it builds the foundation for many advanced tree problems. 🔹 𝗧𝗵𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴 Given the root of a binary tree, find its maximum depth — 👉 the number of nodes along the longest path from root to leaf. 🔹 𝗧𝗵𝗲 𝗞𝗲𝘆 𝗜𝗻𝘀𝗶𝗴𝗵𝘁 💡 This problem is a perfect example of recursion + divide & conquer. At each node: 👉 The depth depends on the maximum depth of its left and right subtree So the idea becomes: Depth = 1 + max(leftDepth, rightDepth) 🔹 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵𝗲𝘀 🔸 Recursive (DFS) • Traverse left and right • Return the max depth from both sides • Add 1 for current node 👉 Clean, intuitive, and most commonly used 🔸 Iterative (BFS - Level Order Traversal) • Use a queue • Traverse level by level • Count number of levels 👉 Useful when you want level-wise processing 🔹 𝗘𝗳𝗳𝗶𝗰𝗶𝗲𝗻𝗰𝘆 ⚡ • Time Complexity → O(n) • Space Complexity → O(h) (recursive stack) Where h is the height of the tree. 🔹 𝗞𝗲𝘆 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆 💡 👉 Tree problems become simple when you think in terms of subproblems at each node 👉 Recursion is not magic — it's just breaking problems into smaller identical pieces 🙏 Huge thanks to my mentors Anchal Sharma and Ikshit .. for guiding me to build strong fundamentals in trees. These basics are the stepping stones to solving complex tree problems. #DSA #Java #100DaysOfCode #BinaryTree #Recursion #DFS #BFS #ProblemSolving #Mentorship #LeetCode #SoftwareEngineering #CodingJourney 🌳🚀
To view or add a comment, sign in
-
-
🚀 LeetCode Progress Update | Strengthening Core Logic & DSA Today, I tackled the classic "String to Integer (atoi)" problem on LeetCode — and what initially seemed straightforward turned out to be a great exercise in handling edge cases and writing robust logic. While implementing the solution, I realized how important it is to think beyond the obvious. The problem involves handling multiple real-world scenarios such as: Leading whitespaces Optional '+' or '-' signs Non-numeric characters Overflow and underflow conditions One of the key learnings from this problem was understanding how 32-bit integer limits work. In Java, an int can store values only within the range: -->( -2³¹ to 2³¹ - 1) i.e., -2,147,483,648 to 2,147,483,647 So, while parsing a number from a string, if the value exceeds this range: It must be clamped to Integer.MAX_VALUE (2147483647) Or Integer.MIN_VALUE (-2147483648) To handle this effectively, I used a larger data type during computation and carefully controlled overflow conditions — which gave me deeper insight into how real-world systems prevent crashes and unexpected behavior. 💡 This problem reinforced: The importance of edge-case handling Writing defensive and scalable code How exception handling (like try-catch) plays a role when dealing with numeric limits Solving this medium-level problem (with a relatively low acceptance rate) definitely boosted my confidence and strengthened my problem-solving approach. Looking forward to solving more such challenges and continuously improving my DSA and logical thinking skills! 💪 Big thanks to LeetCode for providing such a powerful platform for learning and growth. #LeetCode #DSA #Java #ProblemSolving #CodingJourney #SoftwareEngineering #Developers #TechGrowth #100DaysOfCode
To view or add a comment, sign in
-
-
Day 80 of DSA Problem Solving Solved LeetCode 876 — Middle of the Linked List 🔥 Today’s problem was all about linked list traversal and using the two-pointer technique efficiently. 🚀 Problem Idea We are given the head of a singly linked list, and the task is to return the middle node of the linked list. If there are two middle nodes, we return the second middle node. 💡 Key Learning The main insight was: If we use two pointers, one moving 1 step at a time and the other moving 2 steps at a time, then by the time the fast pointer reaches the end of the list, the slow pointer will be standing at the middle node. So instead of counting the total number of nodes first, we can directly find the middle in just one traversal. 🧠 Concepts Practiced Two Pointer Technique Linked List Traversal Fast and Slow Pointer Single Pass Traversal Pointer Movement Logic ⏱ Time Complexity Time: O(n) Space: O(1) 📈 Real Journey Behind the Solution At first, this problem looks very basic, and the brute force idea of counting nodes and then reaching the middle can come to mind quickly. But this question teaches a much smarter and cleaner approach using slow and fast pointers. This problem helped me strengthen my understanding of linked list fundamentals and pointer movement, which are extremely important in coding interviews. Every day, I’m realizing that even easy problems can teach very powerful concepts when solved with the right approach. #Day80 #DSA #LeetCode #Java #LinkedList #TwoPointers #CodingJourney #ProblemSolving #CodingDaily
To view or add a comment, sign in
-
-
Day 78 - LeetCode Journey Solved LeetCode 142: Linked List Cycle II (Medium) today — a powerful extension of the cycle detection problem. Earlier, we learned how to detect if a cycle exists. Now the challenge is to find the exact node where the cycle begins. 💡 Core Idea (Floyd’s Algorithm Extended): 1) Use slow & fast pointers to detect a cycle 2) Once they meet, reset one pointer to the head 3) Move both pointers one step at a time 4) The point where they meet again = start of the cycle 🤯 Why it works? Because of the mathematical relationship between distances traveled inside the cycle — both pointers align perfectly at the cycle entry point. ⚡ Key Learning Points: • Advanced use of two-pointer technique • Understanding the mathematics behind cycle detection • Solving without modifying the list • Maintaining O(n) time and O(1) space This is not just coding — this is algorithmic thinking at a deeper level. Also, this pattern connects with: -> Find duplicate number (cycle in array) -> Happy Number problem -> Loop detection in graphs ✅ Stronger conceptual clarity ✅ Better problem-solving depth ✅ Confidence with tricky pointer problems From detecting a cycle to pinpointing its start — that’s real progress 🚀 #LeetCode #DSA #Java #LinkedList #TwoPointers #Algorithms #ProblemSolving #CodingJourney #Consistency #100DaysOfCode #InterviewPreparation #DeveloperGrowth #KeepCoding
To view or add a comment, sign in
-
-
🚀 Day 27/60 — LeetCode Discipline Problem Solved: Length of Last Word Difficulty: Easy Today’s problem looked simple, yet it emphasized a subtle but important skill — handling edge cases cleanly. The goal was to find the length of the last word in a string, ignoring any trailing spaces. Instead of splitting the string or using extra space, the solution efficiently traverses from the end, skipping unnecessary characters and counting only what truly matters. It’s a reminder that strong coding is not always about complexity — sometimes, it’s about precision and clarity in small details. 💡 Focus Areas: • Practiced string traversal from end • Improved handling of trailing spaces • Reinforced clean and efficient logic • Avoided unnecessary extra space usage • Strengthened edge-case thinking ⚡ Performance Highlight: Achieved 0 ms runtime (100% performance). Small problems, when approached with discipline, sharpen the instincts that solve big ones. #LeetCode #60DaysOfCode #100DaysOfCode #DSA #Strings #Algorithms #ProblemSolving #CodingJourney #SoftwareEngineering #Java #Developers #Consistency #TechJourney #LearnToCode
To view or add a comment, sign in
-
-
Day 53 :- 𝗣𝗮𝘁𝘁𝗲𝗿𝗻 𝗠𝗮𝘁𝗰𝗵𝗶𝗻𝗴 𝗶𝗻 𝗦𝘁𝗿𝗶𝗻𝗴𝘀: 𝗜𝘀𝗼𝗺𝗼𝗿𝗽𝗵𝗶𝗰 𝗦𝘁𝗿𝗶𝗻𝗴𝘀 🔤🔁 Today’s DSA session was about understanding character mapping and pattern consistency using LeetCode 205: Isomorphic Strings. This problem is a great way to strengthen understanding of hashing + mapping patterns in strings. 🔹 𝗧𝗵𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴 Given two strings s and t, determine if they are isomorphic. 👉 Two strings are isomorphic if characters in s can be replaced to get t, while maintaining order. 👉 No two characters can map to the same character (one-to-one mapping). 🔹 𝗧𝗵𝗲 𝗞𝗲𝘆 𝗜𝗻𝘀𝗶𝗴𝗵𝘁 💡 This problem combines: • Character mapping • Tracking last seen positions 👉 Instead of explicitly storing mappings, we track last occurrence index of characters. 🔹 𝗖𝗼𝗿𝗲 𝗟𝗼𝗴𝗶𝗰 ✔️ If lengths are different → return false ✔️ Use two arrays to track last seen positions: • lastS for string s • lastT for string t ✔️ For each index i: • If last occurrence of s[i] ≠ last occurrence of t[i] → not isomorphic • Update both with i + 1 👉 Using i + 1 avoids confusion with default value 0 🔹 𝗪𝗵𝘆 𝗧𝗵𝗶𝘀 𝗪𝗼𝗿𝗸𝘀 👉 Ensures consistent mapping pattern 👉 If two characters behaved differently before → mismatch detected 👉 No need for complex hash maps 🔹 𝗘𝗳𝗳𝗶𝗰𝗶𝗲𝗻𝗰𝘆 ⚡ • Time Complexity → O(n) • Space Complexity → O(1) (fixed size arrays) 🔹 𝗞𝗲𝘆 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆 💡 👉 Pattern problems can often be solved using last seen indices 👉 Always think of simpler representations before jumping to hash maps 👉 Consistency in mapping is more important than actual values 🙏 Huge thanks to my mentors Anchal Sharma and Ikshit .. for guiding me in recognizing patterns in string problems. This approach makes many problems much simpler! #DSA #Java #100DaysOfCode #Strings #Hashing #ProblemSolving #Mentorship #LeetCode #SoftwareEngineering #CodingJourney 🚀
To view or add a comment, sign in
-
Explore related topics
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