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
Maximum Distance Between a Pair of Values in Non-Increasing Arrays
More Relevant Posts
-
🚀 Day 117 of My DSA Problem Solving Journey - The Grind Continues! 🎉 After conquering Monotonic Stacks, today I tackled an interesting array and Hash Map problem on LeetCode: "Minimum Distance Between Three Equal Elements I" in C++. The Problem: Given an array, we need to find three identical elements at distinct indices (i, j, k) such that their "distance" abs(i - j) + abs(j - k) + abs(k - i) is minimized. If no such tuple exists, return -1. My Approach: Instead of a brute-force O(N^3) loop to find triplets, I optimized the solution using a Hash Map (unordered_map)! 🧠 The Logic: Math Simplification: The distance formula abs(i - j) + abs(j - k) + abs(k - i) actually simplifies to 2 \times (k - i) assuming i < j < k. This was the "Aha!" moment! I didn't even need the middle index j for the calculation, just the first and third indices of the triplet. Tracking Indices: I used a hash map where the key is the array element and the value is a vector storing all the indices where this element appears. On-the-Fly Calculation: As I iterate through the array, the moment a number appears for the third time (or more), I grab its current index (i3) and the index from two occurrences ago (i1). I calculate the distance as 2 times (i3 - i1) and update my minimum answer. Sliding Window Feel: By always checking the latest index (n-1) and the index two steps back (n-3), I ensure I'm always calculating the tightest possible distance for any three consecutive occurrences of a number. Takeaway: This problem is a great reminder of how mathematical simplification combined with Hash Maps can turn an intimidating problem into an elegant O(N) Time and Space complexity solution. Keep an eye out for those hidden formulas! ⚡ Keep pushing! 💻🔥 #Day117 #CPP #HashMap #Arrays #LeetCode #DataStructures #Algorithms #DSA #ProblemSolving #CodingJourney #ContinuousLearning #REGexSoftwareServices
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
-
-
Day 101 Today’s problem: Minimum Distance Between Three Equal Elements II This one tested my ability to optimize beyond brute force and really think about patterns in indices. 🔍 Key Insight: Instead of checking all triplets (which would be too slow), I grouped indices of the same values using a hashmap. Then, I observed that for any three equal elements at positions i, j, k, the distance simplifies to: 👉 2 * (k - i) So the goal became finding the minimum window of 3 consecutive indices for each number. 💡 Approach: Store indices of each value Iterate over each list of indices Check consecutive triplets Minimize the distance ⚡ Complexity: Time: O(n) Space: O(n) ✅ Accepted ⏱ Runtime: 371 ms 💾 Memory: 343.4 MB 📌 Takeaway: Sometimes, simplifying the formula is the real optimization. Once you see the pattern, the problem becomes much cleaner. On to Day 102 💪 #LeetCode #DataStructures #Algorithms #CodingJourney #100DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 91/100 – LeetCode Challenge ✅ Problem: 148. Sort List 💡 Approach: Merge Sort on Linked List Today’s problem was a classic example of applying merge sort in a linked list to achieve: ✔ O(n log n) time complexity ✔ Efficient sorting without converting to array 🔍 Key Learnings: Used slow & fast pointer to split the list into halves Applied recursion to sort each half Merged two sorted linked lists efficiently Understood why merge sort is preferred for linked lists over quicksort ⚡ Result: Runtime: 9 ms (Beats 98.63%) Memory: 59.16 MB 💭 Insight: Linked lists don’t support random access, making merge sort the most optimal approach for sorting compared to other algorithms. Consistency > Motivation 💯 Almost nearing the finish line of this 100-day challenge! #Day91 #LeetCode #100DaysOfCode #DataStructures #Algorithms #LinkedList #MergeSort #CodingJourney
To view or add a comment, sign in
-
-
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
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
-
-
-- Solved: Maximum Depth of Binary Tree (LeetCode 104) -- Problem: Given the root of a binary tree, return its maximum depth — the number of nodes along the longest path from the root to a leaf. -- Approach (Recursive DFS): - The idea is simple but powerful: > If the node is NULL → depth is 0 > Recursively compute the depth of left and right subtrees > Return: 1 + max(leftDepth, rightDepth) > This works because at every node, we are asking: “What is the longest path going down from here?” -- Complexity: Time: O(n) Space: O(h) #DSA #LeetCode #Recursion #BinaryTree #ProblemSolving #CodingJourney #algorithm
To view or add a comment, sign in
-
-
Day 36 of DSA Practice ✅ Worked on: • Next Greater Element I (LeetCode 496) Started with a brute force approach using nested loops (i, j, k) to find the next greater element for each number. It worked, but felt inefficient and dependent on repeated comparisons. Then realized this problem can be solved more efficiently using a stack. Traversed nums2 from right to left and used a stack to keep track of greater elements. Popped all smaller elements and mapped each number to its next greater element using a HashMap. Finally used that map to build the answer for nums1. This approach felt much cleaner and reduced unnecessary work compared to brute force. Today’s key learning was understanding how stack can help in solving “next greater” type problems efficiently. #leetcode #datastructures #algorithms #stack #problemsolving #codingpractice #learninpublic #buildinpublic #techjourney #growthmindset
To view or add a comment, sign in
-
-
🚀 Day 87/100 – 4Sum 🧠 Problem: Given an array of integers, return all unique quadruplets [a, b, c, d] such that: 👉 a + b + c + d = target ✔️ No duplicate quadruplets ✔️ Order doesn’t matter 💡 Core Idea Extension of 3Sum → 4Sum 👉 Sort the array 👉 Fix two elements (i, j) 👉 Use two pointers (left, right) 🔥 Key Trick: Skip duplicates at every step to avoid repeated quadruplets 📚 Key Learnings 1. Two pointer optimization 2. Handling duplicates carefully 3. Reducing nested loops using sorting ⏱️ Complexity Time Complexity: O(n³) Space Complexity: O(1) (excluding output) #100DaysOfCode #Day87 #LeetCode #DSA #TwoPointers #Algorithms #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 89/100 – Permutations II 🧠 Problem: Given an array that may contain duplicates, return all unique permutations ✔️ No duplicate permutations ✔️ Order doesn’t matter 💡 Core Idea 🔥 Classic Backtracking + Duplicate Handling 👉 Sort the array first 👉 Use a used[] array 👉 Skip duplicates using condition: if(i > 0 && nums[i] == nums[i-1] && !used[i-1]) continue; ⚡ This ensures only unique permutations are generated 📚 Key Learnings Backtracking with pruning Handling duplicates efficiently Importance of sorting before recursion ⏱️ Complexity Time Complexity: O(n! ) Space Complexity: O(n) #100DaysOfCode #Day89 #LeetCode #DSA #Backtracking #Algorithms #ProblemSolving #CodingJourney #Consistency
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