Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #928: Minimize Malware Spread II

In this guide, we solve Leetcode #928 Minimize Malware Spread II in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

You are given a network of n nodes represented as an n x n adjacency matrix graph, where the ith node is directly connected to the jth node if graph[i][j] == 1. Some nodes initial are initially infected by malware.

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Depth-First Search, Breadth-First Search, Union Find, Graph, Array, Hash Table

Intuition

Fast membership checks and value lookups are the heart of this problem, which makes a hash map the natural choice.

By storing what we have already seen (or counts/indexes), we can answer the question in one pass without backtracking.

Approach

Scan the input once, using the map to detect when the condition is satisfied and to update state as you go.

This keeps the solution linear while remaining easy to explain in an interview setting.

Steps:

  • Initialize a hash map for seen items or counts.
  • Iterate through the input, querying/updating the map.
  • Return the first valid result or the final computed value.

Example

Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1] Output: 0

Python Solution

class UnionFind: __slots__ = "p", "size" def __init__(self, n: int): self.p = list(range(n)) self.size = [1] * n def find(self, x: int) -> int: if self.p[x] != x: self.p[x] = self.find(self.p[x]) return self.p[x] def union(self, a: int, b: int) -> bool: pa, pb = self.find(a), self.find(b) if pa == pb: return False if self.size[pa] > self.size[pb]: self.p[pb] = pa self.size[pa] += self.size[pb] else: self.p[pa] = pb self.size[pb] += self.size[pa] return True def get_size(self, root: int) -> int: return self.size[root] class Solution: def minMalwareSpread(self, graph: List[List[int]], initial: List[int]) -> int: n = len(graph) s = set(initial) uf = UnionFind(n) for i in range(n): if i not in s: for j in range(i + 1, n): graph[i][j] and j not in s and uf.union(i, j) g = defaultdict(set) cnt = Counter() for i in initial: for j in range(n): if j not in s and graph[i][j]: g[i].add(uf.find(j)) for root in g[i]: cnt[root] += 1 ans, mx = 0, -1 for i in initial: t = sum(uf.get_size(root) for root in g[i] if cnt[root] == 1) if t > mx or (t == mx and i < ans): ans, mx = i, t return ans

Complexity

The time complexity is O(n2×α(n))O(n^2 \times \alpha(n))O(n2×α(n)), and the space complexity is O(n2)O(n^2)O(n2). The space complexity is O(n2)O(n^2)O(n2).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy