LeetCode 0928. Minimize Malware Spread II Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
0928. Minimize Malware Spread II

Description

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. Whenever two nodes are directly connected, and at least one of those two nodes is infected by malware, both nodes will be infected by malware. This spread of malware will continue until no more nodes can be infected in this manner.

Suppose M(initial) is the final number of nodes infected with malware in the entire network after the spread of malware stops.

We will remove exactly one node from initial, completely removing it and any connections from this node to any other node.

Return the node that, if removed, would minimize M(initial). If multiple nodes could be removed to minimize M(initial), return such a node with the smallest index.

 

Example 1:

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

Example 2:

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

Example 3:

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

 

Constraints:

  • n == graph.length
  • n == graph[i].length
  • 2 <= n <= 300
  • graph[i][j] is 0 or 1.
  • graph[i][j] == graph[j][i]
  • graph[i][i] == 1
  • 1 <= initial.length < n
  • 0 <= initial[i] <= n - 1
  • All the integers in initial are unique.

Solutions

Solution 1: Union-Find

We can use the union-find data structure to merge all nodes that are not in initial and satisfy graph[i][j] = 1.

Next, we create a hash table g, where g[i] represents the root node of the connected component that is connected to node i. We also need a counter cnt to count how many initial nodes each root node is infected by.

For each initially infected node i, we traverse all nodes j connected to node i. If node j is not in initial, we add the root node of node j to the set g[i]. At the same time, we count how many initial nodes each root node is infected by and save the result in the counter cnt.

Then, we use a variable ans to record the answer, and mx to record the maximum number of infected nodes that can be reduced. Initially, ans = 0, mx = -1.

We traverse all initially infected nodes. For each node i, we traverse all root nodes in g[i]. If a root node is only infected by one initial node, we add the size of the connected component where the root node is located to t. If t > mx or t = mx and i < ans, we update ans = i, mx = t.

Finally, we return ans.

The time complexity is O(n2 × Î±(n)), and the space complexity is O(n2). Where n is the number of nodes, and α(n) is the inverse Ackermann function.

PythonJavaC++GoTypeScript
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(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !