61 of #100DaysOfCode 🚀 Solved LeetCode 907 – Sum of Subarray Minimums using an optimized Monotonic Stack + Contribution Technique 🔥 🔍 Approach: Instead of generating all subarrays, I focused on how each element contributes as the minimum in different subarrays. 👉 For every element: • Find how far it can extend to the left (Previous Smaller Element) • Find how far it can extend to the right (Next Smaller Element) 📌 Using a monotonic increasing stack: • Left array stores distance to previous smaller element • Right array stores distance to next smaller element 💡 Formula Used: Each element contributes: arr[i] * left[i] * right[i] ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(n) #LeetCode #Stack #Algorithms #DataStructures #CodingJourney #ProblemSolving #100DaysOfCode
Solved LeetCode 907 with Monotonic Stack and Contribution Technique
More Relevant Posts
-
LeetCode Practice Problem no.153( Find Minimum in Rotated Sorted Array) Statement - Suppose We are given an array that is sorted in ascending order between 1 to n times , we have to return the minimum element of this array. Approach - Binary Search - Key Idea- -As The array is sorted so Binary Search can be applied -At Least Half of the Array (left or right) will always be sorted -The minimum element lies in the unsorted part if the array -By comparing with the mid element we efficiently narrow down the search space Algorithm 1-Initialize two pointers: low = 0, high = n - 1 While low < high: 2-Compute mid = (low + high) / 2 3-If nums[mid] > nums[high], the minimum lies in the right half → low = mid + 1 4-Else, the minimum lies in the left half (including mid) → high = mid 5-Return nums[low] as the minimum element Time Complexity - O(log(n)) Space Complexity - O(1) #LeetCode #DataStructures #Algorithms #BinarySearch
To view or add a comment, sign in
-
-
Day 90 of #1000DaysOfCoding 🚀 Today’s focus: mastering array manipulation with an optimized approach. Solved the classic “Rotate Array” problem using an in-place reversal technique. Instead of extra space, I used a three-step reverse strategy to achieve O(n) time and O(1) space complexity. Key takeaway: Sometimes the most efficient solution isn’t the most obvious one. Breaking a problem into smaller transformations can lead to clean and optimal code. Progress update: ✔️ 40/40 test cases passed ✔️ 0 ms runtime ✔️ Improved problem-solving intuition Nearing the 100-day milestone. Staying consistent, staying curious. #100DaysOfCode #CodingJourney #LeetCode #DataStructures #Algorithms #CPP #ProblemSolving #SoftwareEngineering #DeveloperLife #CodingChallenge
To view or add a comment, sign in
-
-
Day 109 Solved: 1855. Maximum Distance Between a Pair of Values (Medium) Today’s problem was a solid exercise in two-pointer technique on non-increasing arrays. 🔍 Key Idea: Since both arrays are non-increasing, we can avoid brute force and use a greedy two-pointer approach: Start with i = 0, j = 0 If nums1[i] <= nums2[j], update max distance and move j forward Otherwise, move i forward ⚡ Why it works: We leverage the sorted nature (non-increasing) to ensure we never revisit unnecessary pairs → O(n + m) time complexity. 💡 Learning: Understanding array properties (like sorted order) can completely change your approach—from brute force to optimal. 📈 Progress: Day 109 of consistency. Still learning, still improving. #LeetCode #DataStructures #Algorithms #CodingJourney #100DaysOfCode #TwoPointers
To view or add a comment, sign in
-
-
To solve LeetCode 84 (Largest Rectangle in Histogram) efficiently, we utilize a Monotonic Stack to track bar indices in increasing order of height. The intuition relies on the fact that a bar's maximum rectangle is constrained by the first smaller bar to its left and right. By maintaining a stack where heights always increase, we can process each bar as it's popped, using the current index and the new stack top as the boundaries. This optimizes the search from a naive O(n^2) approach to a linear O(n) time complexity. #LeetCode #SoftwareEngineering #Algorithms #CodingTips
To view or add a comment, sign in
-
Day 58 of solving LeetCode problems. Today’s focus: Range Sum Query – Mutable At first glance, it looks simple. But the moment updates enter the picture, brute force collapses. This problem pushes you toward Segment Trees / Fenwick Trees — where preprocessing and smart updates turn inefficiency into precision. Key takeaway: Speed isn’t about doing things faster. It’s about avoiding unnecessary work entirely. Still consistent. Still refining. There’s always a structure behind the surface. 3 → 6 → 9 #LeetCode #Algorithms #DataStructures #CodingJourney #DSA #SegmentTree #FenwickTree #ProblemSolving #SoftwareEngineering #Consistency #KeepBuilding #DeveloperMindset
To view or add a comment, sign in
-
-
🚀 Day 87 of #100DaysOfCode 🧠 Problem Solved: 1855. Maximum Distance Between a Pair of Values Today’s problem tested my understanding of two-pointer technique on sorted (non-increasing) arrays. 💡 Key Insight: Instead of checking all pairs (which would be slow), we can efficiently move pointers to maximize the distance while maintaining the condition: 👉 nums1[i] ≤ nums2[j] ⚡ Approach Used: - Start with two pointers at the beginning - Expand the right pointer to maximize distance - Adjust the left pointer only when the condition breaks - Track the maximum distance throughout 📈 Result: - Optimized from brute force O(n²) → O(n) - Efficient and clean logic 🔥 What I Learned: - How sorted properties help reduce complexity - Smart pointer movement is key to greedy problems - Avoid brute force when patterns exist Consistency > Motivation 💯 #Day87 #LeetCode #DataStructures #Algorithms #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
LeetCode Daily | Day 77 🔥 LeetCode POTD – 2515. Shortest Distance to Target String in a Circular Array (Easy) ✨ 📌 Problem Insight Given a circular array: ✔ Move left or right from a start index ✔ Each move costs 1 step ✔ Find minimum steps to reach target string ✔ Return -1 if target not found 🔍 Initial Thinking – Simulation ⚙️ 💡 Idea: ✔ Try moving step by step left & right ✔ Stop when target is found ⚠️ Problem: ✔ Circular nature makes indexing tricky ✔ Need to handle wrap-around carefully 💡 Key Observation 🔥 ✔ Distance in circular array = min(direct, wrap-around) ✔ For any index i: → distance = min(|i - start|, n - |i - start|) 🚀 Optimized Approach ✔ Traverse entire array once ✔ For every index matching target: → Compute circular distance ✔ Take minimum of all 🔧 Core Idea ✔ Convert movement → distance calculation ✔ Avoid simulation, use math ✔ Handle circular nature smartly ⏱ Complexity ✔ Time: O(n) ✔ Space: O(1) 🧠 Key Learning ✔ Circular problems often reduce to min(dist, n - dist) ✔ Avoid simulation when direct formula exists ✔ Always check all possible target positions 🚀 Takeaway A simple problem that teaches an important pattern for circular arrays — think mathematically, not procedurally ⚡ #LeetCode #DSA #Algorithms #CPlusPlus #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Today’s LeetCode Solve: 3Sum Closest Today I solved the “3Sum Closest” problem on LeetCode — a great variation of the classic 3Sum problem. 🔍 Problem summary: Given an array and a target value, find three integers such that the sum is closest to the target. 💡 Approach used: --> Sort the array first --> Fix one element and use the two-pointer technique for the remaining --> elements --> Calculate the sum and compare it with the target --> Update the closest sum whenever a better match is found Move pointers based on whether the current sum is less or greater than the target ⚡ Efficiency: --> Time Complexity: O(n²) --> Space Complexity: O(1) 📌 Key takeaway: Small variations in problems (like “closest” instead of exact match) require careful thinking but often reuse the same core pattern. Consistent practice is making patterns clearer day by day 💪 #LeetCode #DSA #TwoPointers #ProblemSolving #Algorithms #CodingPractice #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 11 of My LeetCode Journey Today’s problem: Container With Most Water This problem looked tricky at first, but the solution turned out to be a clean application of the Two Pointer technique. 💡 Key Idea: Start with two pointers at both ends Calculate area using width × minimum height Move the pointer with smaller height to maximize area 🔍 Instead of brute force O(n²), optimized it to O(n) 🚀 ⚡ Result: Accepted ⏱️ Runtime: 60 ms 🧠 What I Learned: Two Pointers can drastically reduce complexity Always think about optimizing from both ends Logic > brute force #LeetCode #DSA #CodingJourney #100DaysOfCode #TwoPointers #Algorithms #ProblemSolving
To view or add a comment, sign in
-
-
After recently using the reversal trick to rotate arrays in O(1) space, I realized the exact same logic applies beautifully to strings. Today I tackled LeetCode 151: Reverse Words in a String in C++. Instead of taking the easy route by allocating extra memory to split the string into a new array of words, I solved it entirely in-place. The strategy: 1️⃣ Reverse the entire string. 2️⃣ Reverse each individual word back to its correct orientation. 3️⃣ Use a two-pointer pass to clean up any leading, trailing, or multiple spaces. Connecting the dots between different data structures and seeing how array pointer techniques translate perfectly to strings is incredibly rewarding. #LeetCode #Cpp #DataStructures #Algorithms #TwoPointers
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