Day 41 :- Exploring Contribution Logic: Sum of Subarray Minimums ✅ Today’s DSA session focused on a unique "Divide and Conquer" approach to a classic monotonic stack problem. I tackled 907. Sum of Subarray Minimums by shifting the focus from individual subarrays to the contribution of each element to the final sum. The Technical Breakdown: Contribution Technique: Instead of generating every possible subarray, I calculated how many subarrays have a specific element arr[minIdx] as their minimum. This is determined by the formula (minIdx - left + 1) * (right - minIdx + 1). Recursive Partitioning: By finding the global minimum in the current range [left, right], I calculated its total contribution and then recursively split the array into two halves: those to the left of the minimum and those to the right. Handling Duplicates: I used a strict "less than" check (arr[i] < arr[minIdx]) to consistently pick the first occurrence of a duplicate. This prevents double-counting subarrays when identical values are present. The Complexity Trade-off: While this recursive approach is highly intuitive and uses the Divide and Conquer paradigm, it can reach O(n^2) in the worst case (like a sorted array). This serves as a perfect conceptual bridge before moving toward the O(n) Monotonic Stack optimization! It's a great exercise in changing your perspective from "finding all subarrays" to "calculating the impact of each element"! 🚀 A huge thanks to my mentor, Anchal Sharma and Ikshit .. for the incredible support and for helping me stay consistent on this journey. Having that accountability makes all the difference! #DSA #Java #100DaysOfCode #DivideAndConquer #Recursion #ProblemSolving #Mentorship #LeetCode #SoftwareEngineering
Exploring Contribution Logic in Sum of Subarray Minimums with Divide and Conquer
More Relevant Posts
-
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
-
-
🚀 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 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 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 🚀
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
-
-
🚀 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
-
-
#Day81 Of Problem Solving Just wrapped up another LeetCode daily challenge — “Minimum Difference Between Highest and Lowest of K Scores.” What made this one interesting wasn’t complexity, but clarity. Sometimes the simplest ideas — like sorting and using a sliding window — can lead to highly efficient solutions. It’s a good reminder that optimization often comes from understanding the problem deeply, not overcomplicating it. Happy to see the solution pass all test cases with strong performance: Runtime: 8 ms (faster than 91.65%) Memory: 46.77 MB Small steps like these are helping me stay consistent and improve problem-solving skills day by day. If you’ve solved this problem differently, I’d love to hear your approach. #LinkedIn #Consistency #Java #DSA #Engineering #LeetCode #HackerRank #ProblemSolving #Sills
To view or add a comment, sign in
-
-
As part of my learning journey in Design Principles… I’ve started exploring SOLID principles, and my first deep dive was into the Single Responsibility Principle (SRP). One simple idea stood out: A piece of code should do only one thing. It sounds basic, but in real-world projects, we often mix multiple responsibilities into a single class — making code harder to maintain, test, and scale. A quick analogy: Think of a Swiss Army Knife vs a single-purpose knife In software, clarity and simplicity always win. One component → One responsibility → Cleaner code My key takeaway: Always ask: “Does this code have more than one reason to change?” If yes — it’s time to refactor. I’ve written a detailed article explaining SRP with examples and practical insights. Read here: https://lnkd.in/gKCtHtu5 Would love to hear your thoughts and feedback! #LearningJourney #CleanCode #DesignPrinciples #SOLID #SoftwareEngineering #Java #AIEngineerJourney
To view or add a comment, sign in
-
-
🚀 Day 40 of My 100 Days Coding Challenge Not every problem is about finding an element — sometimes, it's about finding where it should be. Today’s problem, Search Insert Position, reinforced a key concept in algorithm design: 👉 Efficiency is not just about solving the problem, but solving it the right way. Instead of a linear approach, I implemented an optimal Binary Search solution (O(log n)), ensuring scalability and performance. 💡 What I Focused On Writing an efficient solution rather than a brute-force approach Understanding how boundary conditions impact the result Realizing that the final pointer position can represent the answer 📊 Outcome ✅ All test cases passed ⚡ Runtime: 0 ms (Beats 100%) 💾 Memory Usage: Optimized 🧠 Takeaway Small problems often carry big lessons. Today’s learning reminded me that mastering fundamentals like binary search is crucial for tackling complex challenges ahead. 🔁 Consistency 40 days in — and still going strong 💪 Discipline and consistency are slowly turning into confidence. #100DaysOfCode #Day40 #Java #BinarySearch #LeetCode #DSA #ProblemSolving #SoftwareEngineering #CodingJourney 🚀
To view or add a comment, sign in
-
-
🚀 Day 6 of LeetCode Problem Solving Solved today’s problem — LeetCode #1480: Running Sum of 1d Array 💻🔥 ✅ Approach: Prefix Sum (Running Sum) ⚡ Time Complexity: O(n) 📊 Space Complexity: O(n) The task was to compute the running sum of an array where each element represents the sum of all previous elements including itself. 👉 Example: Input: [1,2,3,4] Output: [1,3,6,10] 💡 Key Idea: Keep adding elements as you traverse the array and store the cumulative sum. 👉 Core Logic: Initialize sum = 0 Traverse array Add current element → sum += nums[i] Store in result array 💡 Key Learning: Simple problems help build strong fundamentals — mastering basics like prefix sum is very important for advanced problems. Grateful to my mentor Pulkit Aggarwal — your guidance is helping me strengthen my fundamentals every day 🙌 Consistency is the key — small steps lead to big results 🚀 #Day6 #LeetCode #DSA #Java #CodingJourney #ProblemSolving #100DaysOfCode
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