🚀 Day 92 of My 100 Days LeetCode Challenge | Java Today’s challenge was a deeper dive into graph algorithms and union–find (disjoint set) concepts. The problem revolved around maximizing the stability of a network while carefully selecting edges. The tricky part was managing connections while avoiding cycles and ensuring the best possible configuration based on constraints. To solve this efficiently, I used the Disjoint Set Union (Union-Find) data structure along with sorting and priority queue logic. This allowed me to dynamically merge components while maintaining optimal edge selection. Instead of brute-forcing all combinations, the union–find structure helped efficiently determine whether two nodes were already connected and whether adding an edge would improve or break the system. ✅ Problem Solved: Maximum Stability of a Network ✔️ All test cases passed (569/569) ⏱️ Runtime: 110 ms 🧠 Approach: Union-Find (Disjoint Set) + Greedy + Priority Queue 🧩 Key Learnings: ● Union-Find is extremely powerful for dynamic connectivity problems. ● Sorting edges strategically can simplify complex graph decisions. ● Avoiding cycles is crucial when building optimal graph structures. ● Priority queues help manage candidate edges efficiently. ● Graph problems often combine multiple data structures together. This problem was a great reminder that graph algorithms are not just about traversal — they’re about managing relationships between components efficiently. 🔥 Day 92 complete — strengthening my understanding of graph optimization and DSU techniques. #LeetCode #100DaysOfCode #Java #Graphs #UnionFind #Algorithms #ProblemSolving #DSA #CodingJourney #Consistency
Maximizing Network Stability with Union-Find and Graph Algorithms
More Relevant Posts
-
🔥 Day 59/100 – LeetCode Challenge 📌 Problem Solved: Remove Duplicates from Sorted Array II (Medium) Today’s problem was a great exercise in in-place array manipulation and two-pointer technique. 💡 Key Idea: Since the array is already sorted, duplicates are adjacent. Instead of removing all duplicates, we allow each element to appear at most twice. 👉 The trick is to compare the current element with the element at index k-2. If they are the same → skip ❌ If different → keep it ✅ ⚙️ Approach: Initialize pointer k = 2 Traverse from index 2 Copy valid elements forward Maintain order without extra space 🧠 What I learned: How to efficiently handle constraints like “at most twice” Importance of thinking in terms of index relationships (k-2) Writing optimal O(n) solutions with O(1) space 📊 Performance: ⚡ Runtime: 0 ms (100%) 💾 Memory: 48.46 MB 💻 Tech Used: Java Consistency is key 🔑 — 59 days done, 41 more to go! #100DaysOfCode #LeetCode #Java #DataStructures #CodingChallenge #ProblemSolving #TechJourney
To view or add a comment, sign in
-
-
🧠 Day 26/75: Logic over Luck When you see a problem that asks for $O(\log n)$ time, your mind should immediately jump to Binary Search. But what if the data isn't perfectly sorted? Today’s LeetCode problem—Search in Rotated Sorted Array—is all about conditions. My approach in Java: Calculate mid safely. Determine if the left half is sorted (nums[left] <= nums[mid]). If yes, check if the target is in that range; otherwise, search the right. If no, it means the right half must be sorted. Result? 0ms Runtime and another problem checked off the list. ✅ Day 26 complete. Every problem is a lesson in thinking more clearly. See you for Day 27! 💻🔥 #Consistency #CodingJourney #LeetCodeChallenge #JavaDeveloper #TechGrowth #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 21/30 – DSA Challenge 📌 LeetCode Problem – Median of Two Sorted Arrays 📝 Problem Statement Given two sorted arrays nums1 and nums2, find the median of the combined array. 📌 Example Input: nums1 = [1,2] nums2 = [3,4] Output: 2.5 💡 My Approach Instead of using complex binary search, I followed a simple and reliable method: 👉 Merge both arrays 👉 Sort the merged array 👉 Find the median This approach is easy to understand and implement. 🚀 Algorithm 1️⃣ Create a new array of size n1 + n2 2️⃣ Copy elements of both arrays 3️⃣ Sort the merged array 4️⃣ If length is odd → return middle element 5️⃣ If even → return average of two middle elements ✅ Java Code import java.util.Arrays; class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int n1 = nums1.length; int n2 = nums2.length; int[] merged = new int[n1 + n2]; for (int i = 0; i < n1; i++) { merged[i] = nums1[i]; } for (int i = 0; i < n2; i++) { merged[n1 + i] = nums2[i]; } Arrays.sort(merged); int n = merged.length; if (n % 2 == 1) { return merged[n / 2]; } else { return (merged[n / 2 - 1] + merged[n / 2]) / 2.0; } } } ⏱ Complexity Time Complexity: O((n + m) log(n + m)) Space Complexity: O(n + m) 📚 Key Learnings – Day 21 ✔ Simple solutions are often easiest to implement ✔ Always understand problem constraints ✔ This problem can be optimized further using binary search ✔ Multiple approaches exist — choose based on context Simple approach. Clear logic. Strong understanding. Day 21 completed. Consistency continues 💪🔥 #30DaysOfCode #DSA #Java #InterviewPreparation #ProblemSolving #CodingJourney #Arrays #LeetCode
To view or add a comment, sign in
-
-
🚀 Day 95 of My 100 Days LeetCode Challenge | Java Today’s challenge was a grid traversal + simulation problem that required careful observation of patterns. The goal was to find the three largest rhombus border sums in a grid. Unlike regular submatrix problems, this one only considers the border elements of rhombus shapes, which makes the calculation a bit more tricky. The approach involved exploring each cell as a potential rhombus center, expanding possible rhombus sizes, and calculating the sum of the boundary elements while ensuring the rhombus stays within grid limits. A TreeSet helped efficiently keep track of the top three unique sums. ✅ Problem Solved: Get Biggest Three Rhombus Sums in a Grid ✔️ All test cases passed (117/117) ⏱️ Runtime: 217 ms 🧠 Approach: Grid Traversal + Simulation + TreeSet 🧩 Key Learnings: ● Grid problems often require careful boundary management. ● Visualizing shapes (like rhombuses) helps simplify implementation. ● TreeSet is useful for maintaining sorted unique values efficiently. ● Simulation-based solutions demand attention to detail. ● Not every grid problem is about rectangles — shapes matter too. This problem was a good reminder that geometric patterns in grids can lead to interesting algorithmic challenges. 🔥 Day 95 complete — improving my grid traversal and pattern-handling skills. #LeetCode #100DaysOfCode #Java #GridProblems #Algorithms #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 31 Today’s focus: Binary Search for boundaries and square roots. Problems solved: • Sqrt(x) (LeetCode 69) • Search Insert Position (LeetCode 35) Concepts used: • Binary Search • Search space reduction • Boundary conditions Key takeaway: In Sqrt(x), the goal is to find the integer square root. Using binary search, we search in the range [1, x] and check mid * mid against x to narrow down the answer. This avoids linear iteration and achieves O(log n) time. In Search Insert Position, we use binary search to find either: • The exact position of the target, or • The correct index where it should be inserted The key idea is that when the target is not found, the final position of the left pointer gives the correct insertion index. These problems highlight how binary search is not just for finding elements, but also for determining positions and boundaries efficiently. Continuing to strengthen fundamentals and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
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
-
-
🚀 Day 17/30– DSA Challenge 📌 LeetCode Problem – Plus One 📝 Problem Statement You are given a large integer represented as an array of digits. Increment the integer by one and return the resulting array. 📌 Example Input: digits = [1,2,3] Output: [1,2,4] 📌 Edge Case Input: [9,9,9] Output: [1,0,0,0] 💡 Key Insight Addition starts from the last digit. 👉 If digit < 9 → just add 1 and return 👉 If digit == 9 → it becomes 0 and carry moves left If all digits are 9 → create a new array. 🚀 Algorithm 1️⃣ Traverse from right → left 2️⃣ If digit < 9: Increment it Return array 3️⃣ Else: Set digit = 0 Continue 4️⃣ If loop ends → all were 9 Create new array of size n+1 Set first element = 1 ✅ Java Code (Optimal O(n)) class Solution { public int[] plusOne(int[] digits) { for (int i = digits.length - 1; i >= 0; i--) { if (digits[i] < 9) { digits[i]++; return digits; } digits[i] = 0; } // All digits were 9 int[] result = new int[digits.length + 1]; result[0] = 1; return result; } } ⏱ Complexity Time Complexity: O(n) Space Complexity: O(1) (except edge case) 📚 Key Learnings – Day 17 ✔ Handle carry carefully ✔ Think from right to left ✔ Edge cases matter more than logic here ✔ Small problems test attention to detail Simple idea. Tricky edge case. Clean implementation. Day 17 completed. Consistency continues 💪🔥 #30DaysOfCode #DSA #Java #InterviewPreparation #ProblemSolving #CodingJourney #Arrays #LeetCode
To view or add a comment, sign in
-
-
🚀 Day 99 of My 100 Days LeetCode Challenge | Java Today’s problem was all about randomization and linked list traversal — a nice break from heavy DP and matrices. The challenge was to design a system that returns a random node’s value from a singly linked list, ensuring that every node has an equal probability of being chosen. Since linked lists don’t allow direct indexing, the key idea was to first determine the size of the list, and then generate a random index to fetch the corresponding node. This approach ensures uniform randomness while keeping the implementation simple and efficient. ✅ Problem Solved: Linked List Random Node ✔️ All test cases passed (8/8) ⏱️ Runtime: 11 ms 🧠 Approach: Linked List Traversal + Randomization 🧩 Key Learnings: ● Randomization problems require ensuring uniform probability distribution. ● Linked lists limit direct access, so traversal becomes essential. ● Precomputing size can simplify random selection. ● Sometimes simple approaches are the most effective. ● Understanding data structure limitations helps design better solutions. This problem highlighted how probability + data structures can come together in elegant ways. 🔥 Day 99 complete — sharpening my understanding of randomization and linked list behavior. #LeetCode #100DaysOfCode #Java #LinkedList #Randomization #Algorithms #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 8 of #100DaysOfCode Solved LeetCode Problem 69 – Sqrt(x) today using a simple loop approach! 🔍 Problem Summary: Given a non-negative integer "x", return the square root of "x" rounded down to the nearest integer (without using built-in functions). 💡 What I practiced: - Basic iteration using loops - Handling overflow using "long" - Understanding how brute force works before optimizing ⚡ Approach (Brute Force): Start from "i = 0" and keep checking: "i * i <= x" Stop when it exceeds "x", and return "i - 1" 💻 Java Code: public static int mySqrt(int x) { long i = 0; while (i * i <= x) { i++; } return (int)(i - 1); } 📌 Takeaway: Starting with a simple loop helps build clarity. Optimization (like Binary Search) can come next! #LeetCode #Java #100DaysOfCode #CodingJourney #BasicsFirst #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 97 of My 100 Days LeetCode Challenge | Java Today’s problem was a solid exercise in matrix processing and prefix sum optimization. The goal was to count the number of submatrices whose sum is less than or equal to a given value (k). A brute-force approach would be too slow, so the key was to use 2D prefix sums to efficiently compute submatrix sums. By converting the matrix into a prefix sum matrix, we can calculate the sum of any submatrix in constant time, making the overall solution much more efficient. ✅ Problem Solved: Count Submatrices With Sum ≤ K ✔️ All test cases passed (859/859) ⏱️ Runtime: 7 ms 🧠 Approach: 2D Prefix Sum 🧩 Key Learnings: ● Prefix sums are powerful for optimizing repeated range sum queries. ● 2D prefix sums extend the same idea from arrays to matrices. ● Preprocessing can drastically reduce computation time. ● Avoiding brute force is key in large input problems. ● Matrix problems often become easier with the right transformation. This problem reinforced how preprocessing techniques like prefix sums can turn complex problems into efficient solutions. 🔥 Day 97 complete — sharpening matrix optimization and prefix sum skills. #LeetCode #100DaysOfCode #Java #PrefixSum #Matrix #Algorithms #ProblemSolving #DSA #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