Alice has just graduated from wizard school, and wishes to cast a magic spell to celebrate. The magic spell contains certain focus points where magic needs to be concentrated, and some of these focus points contain magic crystals which serve as the spell's energy source. Focus points can be linked through directed runes, which channel magic flow from one focus point to another.
You are given a integer n denoting the number of focus points and an array of integers crystals where crystals[i] indicates a focus point which holds a magic crystal. You are also given two integer arrays flowFrom and flowTo, which represent the existing directed runes. The ith rune allows magic to freely flow from focus point flowFrom[i] to focus point flowTo[i].
You need to find the number of directed runes Alice must add to her spell, such that each focus point either:
Return the minimum number of directed runes that she should add.
Example 1:
Input: n = 6, crystals = [0], flowFrom = [0,1,2,3], flowTo = [1,2,3,0]
Output: 2
Explanation:

Add two directed runes:
Example 2:
Input: n = 7, crystals = [3,5], flowFrom = [0,1,2,3,5], flowTo = [1,2,0,4,6]
Output: 1
Explanation:

Add a directed rune from focus point 4 to focus point 2.
Constraints:
2 <= n <= 1051 <= crystals.length <= n0 <= crystals[i] <= n - 11 <= flowFrom.length == flowTo.length <= min(2 * 105, (n * (n - 1)) / 2)0 <= flowFrom[i], flowTo[i] <= n - 1flowFrom[i] != flowTo[i]Problem Overview: You are given runes connected by dependency rules where one rune may require another before it can be used. Some runes are already available. The goal is to determine the minimum number of additional runes you must add so every rune in the system can eventually be activated through the dependency graph.
Approach 1: Direct Reachability Simulation (BFS/DFS from Each Missing Rune) (Time: O(n*(n+m)), Space: O(n+m))
Start by building the directed graph of rune dependencies using adjacency lists. Run BFS or DFS from all initially available runes to mark reachable nodes. For every rune that remains unreachable, simulate adding that rune and run another traversal to see how many additional nodes become reachable. This brute-force style approach helps verify correctness but repeatedly explores the graph, making it inefficient for large inputs. It is useful for understanding how reachability propagates through a dependency graph.
Approach 2: Topological Dependency Tracking (Time: O(n+m), Space: O(n+m))
Treat the runes as nodes in a directed graph and compute indegrees for each node. Perform a traversal similar to topological sort starting from runes that are already available. Each time a rune becomes reachable, decrease the indegree of its neighbors. Nodes that never reach indegree zero remain blocked because none of their prerequisite chains start from an available rune. Counting these independent blocked chains provides a better estimate of how many additional starting runes are needed.
Approach 3: Strongly Connected Components + Condensed Graph (Optimal) (Time: O(n+m), Space: O(n+m))
Cycles in the dependency graph mean several runes must be activated together. Compress the graph into strongly connected components using Kosaraju or Tarjan from Depth-First Search. Each SCC becomes a single node in a condensed DAG. Next, mark all SCCs reachable from the initially available runes using a traversal. In the condensed graph, count SCCs that are not reachable and have zero incoming edges from other unreachable components. Each such component requires adding one rune to start the chain. This works because activating one rune in that component unlocks the entire cycle.
Recommended for interviews: The SCC condensation approach. Interviewers expect candidates to recognize that cycles must be collapsed before analyzing dependency flow. Using graph traversal and topological sort ideas reduces the problem to counting source components in a DAG. Brute-force reachability demonstrates understanding, but the SCC-based solution shows strong graph modeling skills.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Repeated BFS/DFS Reachability | O(n*(n+m)) | O(n+m) | Conceptual understanding or very small graphs |
| Topological Dependency Tracking | O(n+m) | O(n+m) | When the dependency graph behaves like a DAG |
| SCC + Condensed DAG (Optimal) | O(n+m) | O(n+m) | General case with cycles in dependency graph |
Practice Minimum Runes to Add to Cast Spell with our built-in code editor and test cases.
Practice on FleetCode