Day 9/ #100DaysOfCode Solved LeetCode 1848: Minimum Distance to the Target Element. Approach: The problem can be solved using a straightforward linear traversal. Iterate through the array and check for indices where the value equals the target. For each such index, compute the absolute difference between the current index and the given start index. Maintain a variable to track the minimum distance encountered during the traversal. Solution Insight: Initialize a variable with a large value (e.g., Integer.MAX_VALUE). Traverse the array once. Whenever the target element is found, update the minimum distance using Math.abs(i - start). Return the minimum value after the loop. Complexity: Time Complexity: O(n) Space Complexity: O(1) Result: 72/72 test cases passed with optimal runtime performance. This problem reinforces the importance of simple iteration and careful tracking of minimum values in array-based problems. #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #Algorithms
Solved LeetCode 1848: Minimum Distance to Target Element
More Relevant Posts
-
Day 57 of #100DaysOfLeetCode Today’s problem focused on finding the minimum distance between a target element and a given index in an array. I solved it using a simple and efficient approach: Traversed the array once Checked for target occurrences Calculated distance using absolute difference Maintained the minimum value throughout Runtime: 0 ms (Beats 100%) Optimized space usage Key takeaway: Even straightforward iteration can lead to highly optimized solutions when combined with the right logic. #LeetCode #Java #DataStructures #Algorithms #100DaysOfCode #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🧠 Day 36/75: Recursion within Recursion! Today’s challenge was "Subtree of Another Tree," a problem that perfectly illustrates how we can reuse fundamental patterns to solve more complex structures. The Strategy: To solve this, I combined two recursive ideas: Traverse the Main Tree: A standard DFS to visit every node in the primary tree. Structural Comparison: At every node, I used a helper function (isSameTree) to check if the current subtree perfectly matches the target structure. Performance: ✅ Runtime: 3 ms (Beats 67.99% of Java users) ✅ Complexity: $O(M \times N)$ in the worst case, but highly efficient for most tree structures. It’s incredibly satisfying to see yesterday's "Same Tree" logic become just one small part of today's larger solution. Build, repeat, and scale! 🚀 #LeetCode #75DaysOfCode #Java #BinaryTree #Recursion #DSA #SoftwareEngineering #ProblemSolving
To view or add a comment, sign in
-
-
Day 71/100 Completed ✅ 🚀 Solved LeetCode – Reshape the Matrix (Java) ⚡ Implemented an efficient matrix transformation approach by mapping elements from the original matrix to the reshaped matrix using a single traversal. Instead of creating intermediate structures, used index manipulation (count / c, count % c) to place elements correctly while maintaining row-wise order. 🧠 Key Learnings: • Understanding how to map 2D indices into a linear traversal • Efficiently converting between different matrix dimensions • Handling edge cases where reshape is not possible • Writing clean and optimized nested loop logic 💯 This problem strengthened my understanding of matrix traversal and index mapping techniques, which are very useful in array and grid-based problems. 🔗 Profile: https://lnkd.in/gaJmKdrA #leetcode #datastructures #algorithms #java #matrix #arrays #problemSolving #100DaysOfCode 🚀
To view or add a comment, sign in
-
-
Day 72/100 Completed ✅ 🚀 Solved LeetCode – Matrix Diagonal Sum (Java) ⚡ Implemented an efficient approach to calculate the sum of both primary and secondary diagonals of a square matrix in a single traversal. Avoided double-counting the center element by handling the odd-sized matrix case separately. 🧠 Key Learnings: • Efficient diagonal traversal in a matrix • Handling overlapping elements (center in odd n) • Writing optimal O(n) solutions instead of nested loops • Clean and concise index-based logic 💯 This problem improved my understanding of matrix patterns and how to optimize traversal by reducing unnecessary iterations. 🔗 Profile: https://lnkd.in/gaJmKdrA #leetcode #datastructures #algorithms #java #matrix #arrays #problemSolving #100DaysOfCode 🚀
To view or add a comment, sign in
-
-
Day 51 of Daily DSA 🚀 Solved LeetCode 83: Remove Duplicates from Sorted List ✅ Problem: Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Approach: Used a single pointer traversal since the list is already sorted, making duplicate detection straightforward. Steps: Start from head node Compare current node with next node If values are same → skip next node Else move current pointer forward Continue until end of list Return updated head ⏱ Complexity: • Time: O(n) • Space: O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 45.48 MB Sorted input often simplifies the logic — duplicates become adjacent and easy to remove. #DSA #LeetCode #Java #LinkedList #CodingJourney #ProblemSolving #Algorithms
To view or add a comment, sign in
-
-
Day 76/100 Completed ✅ 🚀 Solved LeetCode – Search a 2D Matrix (Java) ⚡ Implemented an optimized binary search approach by treating the 2D matrix as a flattened sorted array. Converted 1D index into 2D coordinates (row = mid / m, col = mid % m) to efficiently locate the target in O(log(m × n)) time. 🧠 Key Learnings: • Applying binary search on a 2D matrix • Converting 1D index to 2D (row & column mapping) • Reducing time complexity from O(m × n) → O(log(m × n)) • Importance of problem observation (matrix behaves like sorted array) 💯 This problem strengthened my understanding of binary search variations and how to apply it beyond simple 1D arrays. 🔗 Profile: https://lnkd.in/gaJmKdrA #leetcode #datastructures #algorithms #java #matrix #binarysearch #arrays #optimization #problemSolving #100DaysOfCode 🚀
To view or add a comment, sign in
-
-
Day 80/90 Solved: Largest Rectangle in Histogram Core idea: → Use monotonic stack to find nearest smaller to left & right → Width = (right - left - 1), maximize area This is one of those problems where the pattern isn’t obvious at first. Once the left/right boundary logic clicks, the solution becomes systematic. #Consistency #DSA #LeetCode #Stack #MonotonicStack #ProblemSolving #Java #Algorithms
To view or add a comment, sign in
-
-
Day 58/100 | #100DaysOfDSA 🔄🔍 Today’s problem: Search in Rotated Sorted Array A twist on Binary Search with a rotated array. Key idea: Even though the array is rotated, one half is always sorted. Approach: • Use binary search • Find mid element • Check which half is sorted (left or right) • Decide if target lies in the sorted half • Narrow the search accordingly Why it works: At every step, we eliminate half of the search space just like standard binary search. Time Complexity: O(log n) Space Complexity: O(1) Big takeaway: Understanding the structure of the problem helps adapt classic algorithms like binary search. Rotation doesn’t break order — it just shifts it. 🔥 Day 58 done. #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearch #Arrays #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 61/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Median of Two Sorted Arrays A classic hard problem with a clever binary search approach. Problem idea: Find the median of two sorted arrays without fully merging them. Key idea: Use binary search on the smaller array to partition both arrays. Why? • We want left half and right half to be balanced • All elements in left ≤ all elements in right How it works: • Pick a cut in array1 • Derive cut in array2 • Check partition validity using boundary elements If valid → we found the median If not → adjust the partition Time Complexity: O(log(min(m, n))) Space Complexity: O(1) Big takeaway: Binary search isn’t just for searching — it can be used to optimize partitions and positions. This one really builds intuition. 🔥 Day 61 done. #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearch #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
🚀 Day 7: Cracking the "Two Pointer" Pattern 🚀 Today, I dived into LeetCode 167 (Two Sum II) to master the Two Pointer technique! While the standard Two Sum problem is often solved with a Hash Map, a Sorted Array gives us a secret advantage. By using two pointers—one at the start and one at the end—we can find the target sum in $O(n)$ time and $O(1)$ space. No extra memory needed! 🧠 💡 Key Takeaway: The magic happens in the movement: Sum < Target? Move the left pointer to grab a larger value. Sum > Target? Move the right pointer to grab a smaller value. Pro Tip: Always watch out for 1-indexed requirements! Adding that +1 to your return indices is the difference between a "Wrong Answer" and "Accepted." ✅ 🛠️ The Logic (Java): Java while (left < right) { int sum = numbers[left] + numbers[right]; if (sum == target) return new int[]{left + 1, right + 1}; else if (sum < target) left++; else right--; } One week down, more patterns to go! Following the roadmap from the "25 DSA Patterns" series. 📈 #DSA #LeetCode #CodingChallenge #Java #TwoPointers #SoftwareEngineering #Consistency
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