Day 103: Simple & Clean 🎯 Problem 1848: Minimum Distance to the Target Element After some complex DP challenges, today was a straightforward exercise in linear search and distance calculation. The Strategy: • Linear Traversal: I iterated through the array to find every occurrence of the target element. • Absolute Minimization: For each match, I calculated the absolute difference between the current index and the start index, keeping track of the minimum value found. Sometimes a simple, O(N) solution is all you need. Day 103 down—maintaining the streak with clarity and consistency. 🚀 #LeetCode #Java #Algorithms #ProblemSolving #DailyCode
Minimum Distance to Target Element in Array
More Relevant Posts
-
Day 2/100: Mastering the Pivot 🔄 Today’s challenge was LeetCode 33: Search in Rotated Sorted Array. The trick here isn't just finding the target—it’s identifying which half of the array is still sorted. Standard Binary Search assumes a perfect slope, but with a rotation, you have to find the "steady ground" before making your move. Constraint: O(\log n) runtime. Key Lesson: Even when data is disrupted (rotated), there is usually a sub-pattern you can exploit to maintain efficiency. One step closer to the goal! 🚀 #100DaysOfCode #Java #DataStructures #Algorithms #LeetCode
To view or add a comment, sign in
-
-
Day 97/200 – LeetCode Challenge Solved “Largest Rectangle in Histogram” (Hard) today. This problem is a great example of how powerful the Monotonic Stack technique can be. Instead of brute force, we efficiently determine how far each bar can extend to compute the maximum rectangle area. Using a monotonic increasing stack to track indices. Identifying left and right boundaries for each bar. Every day is making data structures feel more intuitive! #Day96 #LeetCode #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 571 of #750DaysOfCode 🚀 🔍 Problem Solved: Sum of Distances Today’s problem looked like a classic brute-force trap 👀 At first glance, comparing every pair gives an O(n²) solution — but with constraints up to 10⁵, that’s not going to work. 💡 Key Insight: Instead of comparing all pairs, we can: 👉 Group indices of the same value 👉 Use prefix sums to efficiently calculate distances 🧠 Approach: Group indices by value (using HashMap) For each group: Build prefix sum of indices For each index: Left contribution → i * count - sum Right contribution → sum - i * count Combine both to get final result 📈 Complexity: Time: O(n) Space: O(n) ✨ Takeaway: When you see distance-based problems: 👉 Think in terms of contributions instead of pair comparisons 👉 Prefix sums can turn expensive computations into linear time Another strong pattern added to the toolkit 💪 #LeetCode #DSA #Java #CodingJourney #ProblemSolving #PrefixSum #Algorithms #LearningEveryday
To view or add a comment, sign in
-
-
🚀 Day 32 of #100DaysOfCode Solved LeetCode 179 – Largest Number 🔢 Today’s problem was a great reminder that sorting isn’t always straightforward. Instead of normal numeric sorting, the trick is to compare numbers based on their string concatenation order. 💡 Key Insight: To decide order between two numbers a and b, compare: ab vs ba Whichever forms the larger number should come first. 🔍 What I learned: Custom sorting using comparators Converting integers to strings for flexible comparison Edge case handling (like leading zeros → return "0") ⚡ Approach: Convert integers to strings Sort using (b+a).compareTo(a+b) logic Build the final result using StringBuilder 💻 Efficient and clean solution with strong real-world relevance in greedy + sorting problems #100DaysOfCode #DSA #LeetCode #Java #CodingJourney #ProblemSolving #Algorithms #DSAwithEdSlash @edslash
To view or add a comment, sign in
-
-
Worked on a challenging problem: “Subarrays with K Different Integers” Key takeaway: Instead of directly solving for exactly K distinct elements, I learned a smarter approach: 👉 count(at most K) − count(at most K−1) 🔹 Concepts I practiced: Sliding Window technique HashMap for frequency tracking Two-pointer approach 🔹 What stood out: The idea of counting all valid subarrays ending at each index using (r - l + 1) was really powerful. It completely changed how I think about subarray problems. Always learning, one problem at a time. #DataStructures #Algorithms #Java #LeetCode #SlidingWindow #LearningJourney #ProblemSolving
To view or add a comment, sign in
-
-
💡 Day 61 of LeetCode Problem Solved! 🔧 🌟 482. Find Maximum Consecutive Ones 🌟 🔗 Solution Code: https://lnkd.in/gf5DPq4E 🧠 Approach: • Traverse the binary array once using one pointer. • Keep counting consecutive 1s and update the maximum value at each step. • When a 0 appears, reset the count to 0. ⚡ Key Learning: • This is a simple sliding window idea. No need for nested loops or extra memory. Tracking current count and maximum value is a useful pattern for many array problems. ⏱️ Complexity: • Time: O(N) • Space: O(1) #LeetCode #Java #DSA #ProblemSolving #Consistency #CodingJourney #Algorithms #Arrays
To view or add a comment, sign in
-
-
Day 34/50 🚀 — Score of a String Today’s problem was all about observing patterns in characters and keeping the approach simple. 🔹 Compared ASCII values of adjacent characters 🔹 Used absolute difference to calculate contribution 🔹 Built the result in a single pass (no extra space) Key insight: Sometimes you don’t need complex data structures—just a clean loop and understanding how characters behave as numbers. 💡 Simple logic, implemented cleanly, is often the most efficient solution. Performance: ⚡ Runtime: 1 ms (99%+) 📦 Memory: Efficient #Day34 #50DaysOfCode #Java #DSA #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 567 of #750DaysOfCode 🚀 🔍 Problem Solved: Maximum Distance Between a Pair of Values Today’s challenge was about finding the maximum distance (j - i) such that: ✔️ i ≤ j ✔️ nums1[i] ≤ nums2[j] ✔️ Both arrays are non-increasing 💡 Key Insight: Since both arrays are sorted in descending order, we can avoid brute force and use a Two Pointer approach to achieve optimal performance. 🧠 Approach: Initialize two pointers i and j at 0 If nums1[i] ≤ nums2[j] → valid pair → update distance & move j Else → move i forward Maintain j ≥ i at all times 📊 Complexity: Time: O(n + m) Space: O(1) 🔥 Takeaway: Whenever arrays are sorted, always think of two pointers or binary search before jumping to brute force. This simple shift can reduce complexity from O(n²) → O(n)! #Day567 #750DaysOfCode #LeetCode #Java #DataStructures #Algorithms #TwoPointers #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 66/100 Solved a problem on String Manipulation — Score of a String. 🔍 Problem Insight: Calculated the total score by summing absolute differences between ASCII values of adjacent characters. 💡 Approach: - Iterated through the string once - Used "Math.abs()" to compute differences - Maintained a running sum ⚡ Complexity: Time: O(n) Space: O(1) ✅ Result: All test cases passed Runtime: 1 ms (Beats 99.77%) 📚 Key Learning: Small optimizations and clean logic can lead to highly efficient solutions. Consistency is the key — showing up every day 💪 #Java #DSA #CodingJourney #LeetCode #ProblemSolving #100DaysOfCode
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
-
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