Tap here to switch tabs
Problem
Submissions

Problem: Minimize Malware Spread II

Try to solve the Minimize Malware Spread II problem.

Statement

You are given a network of nn nodes labeled from 00 to n−1n - 1, represented by an n×nn \times n adjacency matrix graph, where graph[i][j] = 1 indicates a direct connection between node i and node j, and graph[i][j] = 0 indicates no connection.

You are also given an array initial containing the nodes initially infected with malware. When two nodes are directly connected and at least one of them is infected, the malware spreads to the other node. This infection process continues until no additional nodes can be infected.

Suppose M(initial)M(\text{initial}) denotes the total number of nodes infected after the spread terminates.

You must remove exactly one node from initial, completely removing that node and all edges connected to it from the network.

Return the node that should be removed to minimize M(initial)M(\text{initial}). If multiple nodes result in the same minimum value, return the node with the smallest index.

Constraints:

  • n ==== graph.length

  • n ==== graph[i].length

  • 2≤2 \leq n ≤300\leq 300

  • graph[i][j] is 00 or 11.

  • graph[i][j] ==== graph[j][i]

  • graph[i][i] ==1== 1

  • 1≤1 \leq initial.length <n< n

  • 0≤0 \leq initial[i] ≤n−1\leq n - 1

  • All the integers in initial are unique.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimize Malware Spread II

Try to solve the Minimize Malware Spread II problem.

Statement

You are given a network of nn nodes labeled from 00 to n−1n - 1, represented by an n×nn \times n adjacency matrix graph, where graph[i][j] = 1 indicates a direct connection between node i and node j, and graph[i][j] = 0 indicates no connection.

You are also given an array initial containing the nodes initially infected with malware. When two nodes are directly connected and at least one of them is infected, the malware spreads to the other node. This infection process continues until no additional nodes can be infected.

Suppose M(initial)M(\text{initial}) denotes the total number of nodes infected after the spread terminates.

You must remove exactly one node from initial, completely removing that node and all edges connected to it from the network.

Return the node that should be removed to minimize M(initial)M(\text{initial}). If multiple nodes result in the same minimum value, return the node with the smallest index.

Constraints:

  • n ==== graph.length

  • n ==== graph[i].length

  • 2≤2 \leq n ≤300\leq 300

  • graph[i][j] is 00 or 11.

  • graph[i][j] ==== graph[j][i]

  • graph[i][i] ==1== 1

  • 1≤1 \leq initial.length <n< n

  • 0≤0 \leq initial[i] ≤n−1\leq n - 1

  • All the integers in initial are unique.