764. Stop the Outbreak
An office network has n computers. graph is an n x n matrix: graph[i][j] == 1 means computers i and j share a cable (the matrix is symmetric and graph[i][i] == 1). A worm starts on every computer listed in initial (all distinct). Each minute every infected computer infects all computers cabled to it, until nothing changes.
Before the worm starts, the administrator unplugs exactly one computer from initial: that computer and all its cables disappear from the network, so it is never infected and cannot pass the worm along. Return the computer to unplug so that the final number of infected computers is as small as possible. If several computers give the same minimum, return the smallest index.
Example 1
- Input:
- graph = [[1,1,0,0,0],[1,1,1,0,0],[0,1,1,0,0],[0,0,0,1,1],[0,0,0,1,1]]initial = [0,3]
- Output:
- 0
- Explanation:
Removing computer 0 saves computers 1 and 2 (two machines), removing 3 saves only computer 4, so 0 is best.
Example 2
- Input:
- graph = [[1,1,0,0],[1,1,1,0],[0,1,1,1],[0,0,1,1]]initial = [0,3]
- Output:
- 0
- Explanation:
The middle computers 1 and 2 are reached from both ends, so removing either infected computer saves nothing and the smaller index 0 wins.
Example 3
- Input:
- graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [1,0]
- Output:
- 0
- Explanation:
Both initial computers are cabled to each other and nothing else is at risk, so the tie goes to the smaller index 0.
Constraints
- 1 ≤ n ≤ 200
graph.length == graph[i].length == n,graph[i][j]is 0 or 1,graph[i][j] == graph[j][i],graph[i][i] == 1- 1 ≤ initial.length ≤ n, entries are distinct and in [0, n - 1]
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 300 msC++ 75 msJava 150 msJavaScript 150 msTypeScript 150 ms
Expected complexity
- Time
- O(n^2)
- Space
- O(n)