Minimum Runes to Add to Cast Spell - Problem

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 an 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 i-th 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:

  • Contains a magic crystal, OR
  • Receives magic flow from another focus point

Return the minimum number of directed runes that she should add.

Input & Output

Example 1 — Basic Magic Spell
$ Input: n = 4, crystals = [0], flowFrom = [1], flowTo = [2]
Output: 2
💡 Note: Focus point 0 has a crystal (satisfied). Focus point 2 receives flow from point 1 (satisfied). Focus points 1 and 3 need additional runes to be satisfied, so answer is 2.
Example 2 — Multiple Crystals
$ Input: n = 3, crystals = [0, 2], flowFrom = [], flowTo = []
Output: 1
💡 Note: Focus points 0 and 2 have crystals (satisfied). Focus point 1 has no crystal and receives no flow, so needs 1 additional rune.
Example 3 — All Satisfied
$ Input: n = 2, crystals = [0], flowFrom = [0], flowTo = [1]
Output: 0
💡 Note: Focus point 0 has a crystal and focus point 1 receives flow from point 0. All points are satisfied, so no additional runes needed.

Constraints

  • 1 ≤ n ≤ 105
  • 0 ≤ crystals.length ≤ n
  • 0 ≤ crystals[i] < n
  • 0 ≤ flowFrom.length = flowTo.length ≤ 105
  • 0 ≤ flowFrom[i], flowTo[i] < n

Visualization

Tap to expand
Minimum Runes to Add to Cast Spell INPUT Focus Points Graph (n=4) 0 crystal 1 2 3 rune n = 4 crystals = [0] flowFrom = [1] flowTo = [2] Node 0 has crystal, 1-->2 exists ALGORITHM STEPS 1 Find Reachable Sets Reverse BFS from crystals 2 Identify Unreached Nodes: {1, 3} need flow 3 Build Reverse Graph Find connected components 4 Count Components Each needs one rune Set Analysis Powered {0, 2} Unpowered {1, 3} 2 separate components = 2 runes needed FINAL RESULT After Adding 2 Runes 0 1 2 3 NEW NEW Output: 2 Minimum Runes All focus points now either: Have crystal OR receive flow [OK] Key Insight: Use reverse graph traversal from crystal nodes to find all powered focus points. Count connected components among unpowered nodes - each component needs exactly one incoming rune from a powered source. This is the optimal set theory approach. TutorialsPoint - Minimum Runes to Add to Cast Spell | Optimal Set Theory Solution
Asked in
Google 15 Amazon 12 Facebook 8
23.4K Views
Medium Frequency
~15 min Avg. Time
892 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