Leetcode Problem 928. Minimize Malware Spread II

928. Minimize Malware Spread II

Leetcode Solutions

Depth First Search to Minimize Malware Spread

  1. Initialize an array infected_by to keep track of the number of nodes that can infect each node.
  2. For each node u in initial, perform a DFS starting from u to find all nodes that can be infected by u.
  3. During the DFS, mark each node as infected by u and increment the count in infected_by for each node reached.
  4. After the DFS, for each node v not in initial, determine if it is uniquely infected by only one node u in initial.
  5. For each node u in initial, calculate the total number of nodes that would be uniquely infected if u were removed.
  6. Find the node u that, if removed, results in the smallest number of total infections. If there are multiple such nodes, return the one with the smallest index.
UML Thumbnail

Union-Find to Minimize Malware Spread

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Code Diffs Compare
Full Screen
Copy Answer Code
Loading...