🚀 Day 24/100 — Problem: Detects if a graph has a cycle Using Disjoint Set (Union-Find) Concept: Union-Find - Each node belongs to a set - Union → connect two nodes - Find → check root parent class UnionFind: def __init__(self, n): self.parent = list(range(n)) def find(self, x): if self.parent[x] != x: self.parent[x] = self.find(self.parent[x]) # path compression return self.parent[x] def union(self, x, y): px, py = self.find(x), self.find(y) if px == py: return True # cycle detected self.parent[py] = px return False What I learned: Efficient grouping helps solve connectivity problems Path compression makes it fast. Time Complexity: ~O(α(n)) #100DaysOfCode #DSA #UnionFind #Graphs #CodingInterview #ProblemSolving #PlacementPrep #LearnInPublic
Detecting Cycles in Graphs with Union-Find
More Relevant Posts
-
Day 67/150 🚀 LeetCode 912: Sort an Array 🧠 Problem Given an integer array nums, sort the array in ascending order without using built-in sorting functions. 📌 Example Input → nums = [5,2,3,1] Output → [1,2,3,5] 💡 Approach (Merge Sort) • Divide array into two halves • Recursively sort left half • Recursively sort right half • Merge sorted halves ⚡ Example Walkthrough [5,2,3,1] → Divide → [5,2] [3,1] → Sort → [2,5] [1,3] → Merge → [1,2,3,5] ⏱ Time Complexity O(n log n) 📦 Space Complexity O(n) ✅ Result: Accepted ⚡ Runtime: 70 ms (Beats 60.61%) Strengthening sorting fundamentals with Merge Sort 🔥 Consistency continues — Day 67 complete 🚀 #Day67 #LeetCode #DSA #MergeSort #Sorting #CodingJourney #ProblemSolving #SoftwareEngineering #TopInterview150
To view or add a comment, sign in
-
-
🚀 #LeetCode Day Problem Solving 🚀 Day-24 📌 Problem: Range Update with Step & XOR You are given an array nums[] and multiple queries. Each query contains: [l, r, k, v] 👉 For every query: • Start from index l • Jump with step k → l, l+k, l+2k ... ≤ r • Update each visited index: nums[i] = (nums[i] * v) % (10⁹ + 7) After processing all queries, return: 👉 XOR of all elements in the array 🛠 Approach: ✔ Direct Simulation 👉 For each query: for i from l to r step k: nums[i] = (nums[i] * v) % MOD ✔ After all updates: 👉 Compute XOR of entire array 🧠 Key Learning: ✔ Handling range with step (k jump) ✔ Modular multiplication (10^9 + 7) ✔ Combining simulation + bitwise XOR 📊 Complexity: ⏱ Time: O(q * (n/k)) → worst case O(n * q) 📦 Space: O(1) 🧪 Example: Input: nums = [2,3,1,5,4] queries = [[1,4,2,3],[0,2,1,2]] After operations → [4,18,2,15,4] Output: 4 ^ 18 ^ 2 ^ 15 ^ 4 = 31 ✅ Day 24 Completed 🚀 Practicing Simulation + Bit Manipulation 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
🚀 Day 9/100 — 💻 Problem: Detect Cycle in a Directed Graph Given a graph, check if it contains a cycle Concept: DFS + Recursion Stack — Traverse each node using DFS — Track visited nodes — Maintain a recursion stack — If a node is visited again in the same path → cycle detected def has_cycle(graph): visited = set() rec_stack = set() def dfs(node): if node in rec_stack: return True if node in visited: return False visited.add(node) rec_stack.add(node) for neighbor in graph[node]: if dfs(neighbor): return True rec_stack.remove(node) return False for node in graph: if dfs(node): return True return False What I learned: Not all problems are about values… some are about relationships. Tracking the path is as important as visiting nodes Time Complexity: O(V + E) Graph problems are all about how things are connected! #100DaysOfCode #DSA #Graphs #DFS #CodingInterview #ProblemSolving #PlacementPrep
To view or add a comment, sign in
-
-
🚀 Day 28/100 — Problem: Minimum Spanning Tree (Prim’s Algorithm) Find the minimum cost to connect all nodes in a graph Concept: Greedy + Priority Queue - Start from any node - Always pick the smallest edge - Expand the tree step by step import heapq def prim(n, graph): visited = set() min_heap = [(0, 0)] # (weight, node) total_cost = 0 while min_heap: weight, node = heapq.heappop(min_heap) if node in visited: continue visited.add(node) total_cost += weight for neighbor, w in graph[node]: if neighbor not in visited: heapq.heappush(min_heap, (w, neighbor)) return total_cost What I learned: Greedy works when choosing the best local option builds the global solution Priority queue helps efficiently select the next best edge Time Complexity: O(E log V) #100DaysOfCode #DSA #Graphs #PrimsAlgorithm #CodingInterview #ProblemSolving #PlacementPrep #LearnInPublic
To view or add a comment, sign in
-
-
🚀 LeetCode Day Problem Solving 🚀 Day-42 📌 Problem: Range Update with Step & XOR You are given an array nums[] and multiple queries. Each query contains: [l, r, k, v] 👉 For every query: • Start from index l • Jump with step k → l, l+k, l+2k ... ≤ r • Update each visited index: nums[i] = (nums[i] * v) % (10⁹ + 7) After processing all queries, return: 👉 XOR of all elements in the array 🛠 Approach: ✔ Direct Simulation 👉 For each query: for i from l to r step k: nums[i] = (nums[i] * v) % MOD ✔ After all updates: 👉 Compute XOR of entire array 🧠 Key Learning: ✔ Handling range with step (k jump) ✔ Modular multiplication (10^9 + 7) ✔ Combining simulation + bitwise XOR 📊 Complexity: ⏱ Time: O(q * (n/k)) → worst case O(n * q) 📦 Space: O(1) 🧪 Example: Input: nums = [2,3,1,5,4] queries = [[1,4,2,3],[0,2,1,2]] After operations → [4,18,2,15,4] Output: 4 ^ 18 ^ 2 ^ 15 ^ 4 = 31 ✅ Day 42 Completed 🚀 Practicing Simulation + Bit Manipulation 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
To view or add a comment, sign in
-
-
𝗜𝗧𝗘𝗥𝗔𝗧𝗜𝗡𝗚 𝗔𝗥𝗥𝗔𝗬𝗦: 𝗞𝗘𝗬 𝗠𝗘𝗧𝗛𝗢𝗗𝗦 You need to work with arrays in your code. Here are key methods to help you: - forEach: This method iterates over an array. It takes a function as an argument to perform operations on each item. You need an object to call this method. It takes three arguments: the item value, the item index, and the array itself. forEach does not return anything. - map: Unlike forEach, map returns a new array. - flatMap: This method transforms each element into an array, then flattens it one level. - filter: This method creates a new array with filtered values based on a condition provided through a callback function. - reduce: This method reduces an array to a single value. It takes four arguments: the total (initial or previously returned value), the item value, the item index, and the array itself. Source: https://lnkd.in/gqq4hDtU
To view or add a comment, sign in
-
🎲📊 Can you solve this Worldquant Question? In this puzzle, each color is assigned a unique three-digit code that resembles a binary sequence. For instance, you are given the following mappings: WHITE = 000 RED = 101 BLUE = 110 PURPLE = 100 Based solely on the pattern observed in these examples, determine the three-digit binary sequence that should be assigned to YELLOW. 💡 Hint: Consider the position of commonalities between letters and numbers. Write your solution in the comments ⬇️
To view or add a comment, sign in
-
Day 180/365 – DSA Challenge 🔺 Solved Pascal’s Triangle II on LeetCode today. 🔹 Problem: Given an index rowIndex, return the corresponding row of Pascal’s Triangle. 🔹 Approach Used: Iterative Row Construction Steps: 1️⃣ Start with base row → [1] 2️⃣ Build each next row using previous row 3️⃣ First & last elements always 1 4️⃣ Middle elements → sum of two elements from previous row 👉 newRow[j] = prevRow[j-1] + prevRow[j] 🔹 Example: rowIndex = 3 → [1,3,3,1] 🔹 Time Complexity: O(n²) 🔹 Space Complexity: O(n) 🔹 Key Insight: Instead of building the full triangle, just keep updating a single row → efficient and clean. 💻 Language: C++ Simple but powerful concept using combinatorics + DP thinking 🚀 #Day180 #365DaysOfCode #DSA #LeetCode #PascalTriangle #DynamicProgramming #Cpp
To view or add a comment, sign in
-
-
From O(n³) to O(n²) — this problem really tested my thinking 👇 Day 3/100 ✅ 📌 Problem: 3Sum (Triplets Sum to Zero) 📌 Pattern: Two Pointer Find all unique triplets in an array that sum to 0. 💡 Brute Force: Check all possible triplets ⏱ Time: O(n³) ⚡ Optimal Approach (Two Pointer): Sort the array Fix one element (i) Use two pointers (L, R) for remaining If sum < 0 → move L If sum > 0 → move R If sum == 0 → store triplet ✅ 👉 Skip duplicates to avoid repeated answers ⏱ Time: O(n²) 📦 Space: O(1) (excluding output) 🔥 Key Insight: Sorting helps apply Two Pointer and efficiently handle duplicates. 👉 Reduced complexity from O(n³) → O(n²) 🧠 What I Learned: Whenever: Triplet / pair problems Need unique combinations 👉 Think sorting + Two Pointer C++ code in comments 👇 If you're also preparing, comment “IN” 💪 #100DaysOfDSA #TwoPointer #3Sum #LeetCode #Cplusplus #ProblemSolving
To view or add a comment, sign in
-
-
🚀 #LeetCode Day Problem Solving 🚀 Day-23 📌 Problem: Range Update with Step & XOR You are given an array nums[] and multiple queries. Each query contains: [l, r, k, v] 👉 For every query: • Start from index l • Jump with step k → l, l+k, l+2k ... ≤ r • Update each visited index: nums[i] = (nums[i] * v) % (10⁹ + 7) After processing all queries, return: 👉 XOR of all elements in the array 🛠 Approach: ✔ Direct Simulation 👉 For each query: for i from l to r step k: nums[i] = (nums[i] * v) % MOD ✔ After all updates: 👉 Compute XOR of entire array 🧠 Key Learning: ✔ Handling range with step (k jump) ✔ Modular multiplication (10^9 + 7) ✔ Combining simulation + bitwise XOR 📊 Complexity: ⏱ Time: O(q * (n/k)) → worst case O(n * q) 📦 Space: O(1) 🧪 Example: Input: nums = [2,3,1,5,4] queries = [[1,4,2,3],[0,2,1,2]] After operations → [4,18,2,15,4] Output: 4 ^ 18 ^ 2 ^ 15 ^ 4 = 31 ✅ Day 23 Completed 🚀 Practicing Simulation + Bit Manipulation 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #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