Day 101: Clean Code is Scalable Code 🚀 Problem 3741: Minimum Distance Between Three Equal Elements II They say if you solve a problem correctly once, the "Hard" version becomes easy. Today was proof of that. The Strategy: • Reusable Logic: My solution from yesterday handled the transition to Part II perfectly. The core logic of tracking indices in a HashMap and applying a sliding window of three was already optimal. • The "Part II" Test: Even with increased constraints or complexity in the problem description, O(N) frequency tracking and index-gap calculations proved to be the robust way to go. • Efficiency: By isolating only the elements that appeared 3+ times, I kept the runtime minimal and the memory footprint low. It's a great feeling when the architecture you built yesterday is strong enough to handle today's challenge without a single line of refactoring. Day 101 and the momentum is only increasing. ⚡ #LeetCode #Java #Algorithms #DataStructures #ProblemSolving #DailyCode
Day 101: Scalable Code with Reusable Logic
More Relevant Posts
-
Day 103 - LeetCode Journey Solved LeetCode 232: Implement Queue using Stacks ✅ Classic problem where you reverse the thinking. Queue is FIFO, but stacks are LIFO… So the trick is to use two stacks to simulate queue behavior. Approach used: Push → reverse elements using second stack Pop/Peek → operate directly from main stack Key learnings: • Understanding stack vs queue behavior • Using two stacks to reverse order • Designing data structures from scratch • Thinking in terms of operations, not just code ✅ All test cases passed ⚡ Efficient and clean implementation This is one of those problems that builds strong fundamentals 💪 #LeetCode #DSA #Stack #Queue #Java #CodingJourney #ProblemSolving #InterviewPrep
To view or add a comment, sign in
-
-
🚀 Day 34 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Maximum Product of Two Digits Problem Insight: Given a number, find the maximum product of any two digits in it. Approach: • Traverse the number digit by digit • Keep track of the two largest digits (large1 and large2) • Multiply the two largest digits to get the answer • No extra data structures required, simple comparison logic Time Complexity: • O(log n) — proportional to the number of digits Space Complexity: • O(1) — only a few variables used Key Learnings: • Simple comparison logic can replace sorting or extra arrays • Breaking down the number digit by digit is often sufficient • Edge cases like single-digit numbers should be considered Takeaway: Finding the largest values while iterating can simplify problems and reduce complexity. #DSA #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving #MathTricks
To view or add a comment, sign in
-
-
🚀 Day 38 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Maximum Product of Two Elements in an Array. Problem Insight: Given an integer array, the goal is to find two elements such that: (nums[i] - 1) * (nums[j] - 1) is maximized Approach: • First, sort the array using Arrays.sort() • Use two nested loops to check all possible pairs • For each pair, calculate → (nums[i] - 1) * (nums[j] - 1) • Keep track of the maximum product Time Complexity: • O(n²) — due to nested loops Space Complexity: • O(1) — no extra space used Key Learnings: • Understanding operator precedence is very important in expressions • Sorting helps in simplifying many problems • Even simple problems can have optimized solutions beyond brute force Takeaway: Brute force helps in understanding the problem deeply, but optimization (like using the two largest elements directly) makes the solution efficient 🚀 #DSA #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving #Arrays
To view or add a comment, sign in
-
-
🚀 Day 547 of #750DaysOfCode 🚀 🔥 Solved: Check if Strings Can be Made Equal With Operations II (LeetCode Medium) 💡 Problem Insight We can swap characters at indices i and j such that: 👉 (j - i) is even 🧠 Key Observation If (j - i) is even ⇒ 👉 indices belong to the same parity group So: Even indices form one group Odd indices form another group 👉 We can rearrange freely within each group 🚫 But cannot mix between them ⚡ Optimized Approach (Single Array Trick) Instead of using two arrays, we can: Use a single frequency array of size 52 First 26 → even indices Next 26 → odd indices 👉 Clever use of: int off = (i & 1) * 26; i & 1 = 0 → even → offset 0 i & 1 = 1 → odd → offset 26 🚀 Day 547 of #750DaysOfCode 🔥 Solved: Check if Strings Can be Made Equal With Operations II (LeetCode Medium) 💡 Problem Insight We can swap characters at indices i and j such that: 👉 (j - i) is even 🧠 Key Observation If (j - i) is even ⇒ 👉 indices belong to the same parity group So: Even indices form one group Odd indices form another group 👉 We can rearrange freely within each group 🚫 But cannot mix between them ⚡ Optimized Approach (Single Array Trick) Instead of using two arrays, we can: Use a single frequency array of size 52 First 26 → even indices Next 26 → odd indices 👉 Clever use of: int off = (i & 1) * 26; i & 1 = 0 → even → offset 0 i & 1 = 1 → odd → offset 26 📈 Complexity Time: O(n) Space: O(1) 💬 Key Takeaway This problem is a great example of: 👉 Index grouping + frequency balancing And the optimization shows: 👉 How bit manipulation (i & 1) + offset trick can reduce space & simplify logic 🔥 🔁 Small optimizations → Big impact over time Consistency continues 💯 #LeetCode #Algorithms #DataStructures #Java #ProblemSolving #CodingChallenge #750DaysOfCode #Consistency
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟓𝟑 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on finding the maximum product subarray. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Maximum Product Subarray 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 • Tracked two values at each step: • Current maximum product • Current minimum product • Why both? • A negative number can turn a small product into a large one • For each element: • Calculated new max and min using previous values • Updated the result with the maximum product found 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Negative numbers can flip the result completely • Tracking both max and min is crucial • DP can be optimized using variables instead of arrays • Edge cases (like zero) reset the product 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n) • Space: O(1) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Sometimes the minimum value is just as important as the maximum — because it might become the next maximum. 53 days consistent 🚀 On to Day 54. #DSA #Arrays #DynamicProgramming #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
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 76 — Slow & Fast Pointer (Find the Duplicate Number) Continuing the cycle detection pattern — today I applied slow‑fast pointers to an array problem where the values act as pointers to indices. 📌 Problem Solved: - LeetCode 287 – Find the Duplicate Number 🧠 Key Learnings: 1️⃣ The Problem Twist Given an array of length `n+1` containing integers from `1` to `n` (inclusive), with one duplicate. We must find the duplicate without modifying the array and using only O(1) extra space. 2️⃣ Why Slow‑Fast Pointer Works Here - Treat the array as a linked list where `i` points to `nums[i]`. - Because there’s a duplicate, two different indices point to the same value → a cycle exists in this implicit linked list. - The duplicate number is exactly the entry point of the cycle (same logic as LeetCode 142). 3️⃣ The Algorithm in Steps - Phase 1 (detect cycle): `slow = nums[slow]`, `fast = nums[nums[fast]]`. Wait for them to meet. - Phase 2 (find cycle start): Reset `slow = 0`, then move both one step at a time until they meet again. The meeting point is the duplicate. 4️⃣ Why Not Use Sorting or Hashing? - Sorting modifies the array (not allowed). - Hashing uses O(n) space (not allowed). - Slow‑fast pointer runs in O(n) time and O(1) space — perfect for the constraints. 💡 Takeaway: This problem beautifully demonstrates how the slow‑fast pattern transcends linked lists. Any structure where you can define a “next” function (here: `next(i) = nums[i]`) can be analyzed for cycles. Recognizing this abstraction is a superpower. No guilt about past breaks — just another pattern mastered, one day at a time. #DSA #SlowFastPointer #CycleDetection #FindDuplicateNumber #LeetCode #CodingJourney #Revision #Java #ProblemSolving #Consistency #GrowthMindset #TechCommunity #LearningInPublic
To view or add a comment, sign in
-
🚀 Day 563 of #750DaysOfCode 🚀 📌 Problem Solved: Shortest Distance to Target String in a Circular Array Today I explored a clean and optimized approach to solving a circular array problem 🔄 💡 Key Insight: Instead of checking all indices, we can expand from the start index in both directions simultaneously 👉 At each step i, we check: Forward → (start + i) % n Backward → (start - i + n) % n ⏱️ The moment we find the target, we return i → which is guaranteed to be the minimum distance 🧠 Why this works: We are exploring layer by layer (like BFS on array) First match = shortest path ✅ No need to scan entire array unnecessarily 🔥 What I Learned: Circular problems can often be solved using modulo arithmetic Expanding outward is more efficient than brute force Think in terms of minimum steps, not positions Consistency is the real game changer 💯 On to Day 564 🚀 #LeetCode #Java #Algorithms #DataStructures #CodingJourney #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🔥 Day 61/100 — #100DaysDSAChallenge Today I worked on a classic problem: Implement Queue using Stacks. 💡 The key idea We use two stacks: 1. One stack for input (push operations) 2. One stack for output (pop/peek operations) When we need to remove an element: If the output stack is empty, we move all elements from input stack to output stack This reverses the order Now the oldest element is on top and ready to be removed ⚙️ Complexity ⏱️ Amortized Time: O(1) 💾 Space: O(n) 🧠 What I learned today Today’s lesson was about reversing data flow to achieve a different behavior. #Java #DSA #Stack #Queue #LeetCode #ConsistencyCurve
To view or add a comment, sign in
-
-
Day 36/50 🚀 Solved “Valid Parentheses” today — a classic stack problem, but a great reminder that simple concepts can be powerful when applied correctly. 💡 Key takeaway: Using a stack makes it easy to track opening brackets and validate matching pairs efficiently. Every closing bracket should correspond to the most recent unmatched opening one — LIFO in action. ⚙️ What I focused on: Clean conditional checks Avoiding unnecessary complexity Writing readable, structured code 📈 Result: Accepted ✅ Optimized runtime & solid performance On to Day 37 🔥 #50DaysOfCode #DataStructures #Algorithms #Java #LeetCode #CodingJourney
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