Imagine you're a cybersecurity expert dealing with a malware outbreak in a computer network! You have a network of n computers represented as an n ร— n adjacency matrix graph, where graph[i][j] == 1 means computers i and j are directly connected.

Some computers in the initial array are already infected with malware. The malware spreads according to this rule: whenever two computers are directly connected and at least one is infected, both become infected. This continues until no more computers can be infected.

Here's your mission: You can quarantine exactly one computer from the initial infected list before the malware starts spreading. Your goal is to find which computer to quarantine to minimize the total number of infected computers after the spread stops.

If multiple computers would result in the same minimum infection count, return the one with the smallest index. Note that a quarantined computer might still get infected later if the malware reaches it through other paths!

Input & Output

example_1.py โ€” Small Network
$ Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
โ€บ Output: 0
๐Ÿ’ก Note: Nodes 0 and 1 are connected, and node 2 is isolated. If we remove node 0, node 1 still spreads to node 0 later, infecting 2 total. If we remove node 1, node 0 still spreads to node 1, infecting 2 total. Since both result in the same infection count, we return the smaller index: 0.
example_2.py โ€” Multiple Components
$ Input: graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,2]
โ€บ Output: 0
๐Ÿ’ก Note: Each node is in its own component (no connections). Removing either node 0 or 2 results in 1 infection instead of 2. Since both give the same result, we return the smaller index: 0.
example_3.py โ€” Optimal Removal
$ Input: graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2]
โ€บ Output: 1
๐Ÿ’ก Note: All nodes are connected in one component. No matter which initially infected node we remove, the malware will still spread through the other initially infected node to infect all 3 nodes. Since the result is the same, return the smaller index: 1.

Constraints

  • n == graph.length == graph[i].length
  • 2 โ‰ค n โ‰ค 300
  • graph[i][j] is 0 or 1
  • graph[i][i] == 1
  • graph[i][j] == graph[j][i]
  • 1 โ‰ค initial.length โ‰ค n
  • All integers in initial are unique

Visualization

Tap to expand
๐Ÿฅ Hospital Outbreak Containment StrategyWard A (High Risk)๐Ÿ˜ท๐Ÿ˜ท๐Ÿ˜ŠWard B (Saveable!)๐Ÿ˜ท๐Ÿ˜Š๐Ÿ˜ŠWard C (Isolated)๐Ÿ˜ท๐ŸŽฏ Strategic Analysis:โ€ข Ward A: 2 infected โ†’ No benefit from quarantine (infection continues)โ€ข Ward B: 1 infected โ†’ Quarantine saves 2 healthy patients!โ€ข Ward C: 1 infected โ†’ Quarantine saves 0 (already isolated)๐Ÿ† Optimal StrategyQuarantine Ward B patient๐Ÿ’ก Key Insight: Maximum impact comes from isolating the ONLY infected person in a connected group!
Understanding the Visualization
1
Map the Network
Identify all connected areas (hospital wards) where the infection can spread
2
Analyze Infected Areas
For each ward, count how many patients are already infected
3
Find Strategic Quarantine
Look for wards with exactly one infected patient - quarantining them saves the entire ward
4
Maximize Impact
Among all strategic quarantines, choose the one that saves the most people
Key Takeaway
๐ŸŽฏ Key Insight: Use Union-Find to identify connected components, then quarantine the infected node that's alone in its component and saves the most people. This transforms a complex simulation into an elegant graph analysis problem!
Asked in
Google 35 Meta 28 Amazon 22 Microsoft 18
28.5K Views
Medium Frequency
~25 min Avg. Time
980 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