Minimum Runes to Add to Cast Spell - Problem

Alice has just graduated from wizard school and wants to cast a magical spell to celebrate! ๐Ÿง™โ€โ™€๏ธโœจ

The spell consists of n focus points (numbered from 0 to n-1), where magic energy flows through a network of directed runes. Some focus points contain magic crystals that serve as energy sources, while others must receive magic flow from connected points.

You are given:

  • n: number of focus points
  • crystals[]: array of focus point indices that contain magic crystals
  • flowFrom[] and flowTo[]: existing directed runes where magic flows from flowFrom[i] to flowTo[i]

For the spell to work, every focus point must either:

  1. Contain a magic crystal, OR
  2. Receive magic flow from another focus point via a rune

Goal: Find the minimum number of additional directed runes Alice needs to add so that every focus point satisfies one of the above conditions.

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 4, crystals = [0], flowFrom = [0], flowTo = [1]
โ€บ Output: 2
๐Ÿ’ก Note: Focus point 0 has crystal, point 1 receives flow from 0. Points 2 and 3 need magic flow, so we need 2 additional runes to connect them to the crystal network.
example_2.py โ€” Chain Connection
$ Input: n = 3, crystals = [0], flowFrom = [0, 1], flowTo = [1, 2]
โ€บ Output: 0
๐Ÿ’ก Note: All points are connected in a chain: 0โ†’1โ†’2. Point 0 has crystal, point 1 receives from 0, point 2 receives from 1. No additional runes needed.
example_3.py โ€” Multiple Components
$ Input: n = 6, crystals = [0, 3], flowFrom = [1, 4], flowTo = [2, 5]
โ€บ Output: 2
๐Ÿ’ก Note: Points 0,3 have crystals. Point 2 receives from 1, point 5 receives from 4. Points 1 and 4 need connections to crystal-bearing points. Need 2 runes: connect 1 to 0, and 4 to 3.

Visualization

Tap to expand
๐Ÿ’ŽF1F2F3F4Crystal SourceConnected NetworkIsolated ComponentNew Rune Needed๐ŸŽฏ Solution Process1. DFS from crystal finds F1, F2 are reachable2. F3, F4 form isolated component3. Add 1 rune: F1โ†’F3 connects isolated componentResult: 1 additional rune needed
Understanding the Visualization
1
Map the Network
Identify all crystal sources and existing rune connections
2
Trace Power Flow
Use DFS to find all focus points reachable from crystals
3
Find Isolated Areas
Identify focus points that cannot reach any crystal source
4
Connect Efficiently
Add minimum runes to connect isolated components to the main network
Key Takeaway
๐ŸŽฏ Key Insight: Use graph traversal to identify reachable vs isolated components, then connect them with minimal new runes

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n + m)

n is focus points, m is existing runes - single DFS traversal

n
2n
โœ“ Linear Growth
Space Complexity
O(n + m)

Store adjacency list and visited arrays for DFS

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 0 โ‰ค crystals.length โ‰ค n
  • 0 โ‰ค crystals[i] < n
  • 0 โ‰ค flowFrom.length = flowTo.length โ‰ค min(n*(n-1), 105)
  • 0 โ‰ค flowFrom[i], flowTo[i] < n
  • flowFrom[i] โ‰  flowTo[i]
  • All crystal indices are unique
  • No duplicate runes in input
Asked in
Google 23 Amazon 18 Meta 15 Microsoft 12
26.2K Views
Medium-High Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen