|
| 1 | +""" |
| 2 | +Lets start from a node in the initial. DFS through the graph. [0] |
| 3 | +When a node is infected, we paint it by color1. |
| 4 | +After the DFS is done, we start from another node in the initial. DFS through the graph. |
| 5 | +When a node is infected, we paint it by color2. |
| 6 | +... |
| 7 | +
|
| 8 | +We don't paint the node that we already colored. [1] |
| 9 | +The more node that are colored by an initial node the more affective it will minimize the malware. [2] |
| 10 | +But if a color have two or more initial node, they won't make any difference if taken away. [3] |
| 11 | +Because those node are still going to be infected by one another initial node. |
| 12 | +
|
| 13 | +So our goal here is to find the initial node that paint the most. |
| 14 | +But did not paint other intial node. |
| 15 | +
|
| 16 | +I use 'color_data' to store the result [4] |
| 17 | +{ |
| 18 | + color1: [ |
| 19 | + [intial nodes in this color], |
| 20 | + the number of node in this color |
| 21 | + ], |
| 22 | + color2: [ |
| 23 | + ... |
| 24 | + ], |
| 25 | + color3: [ |
| 26 | + ... |
| 27 | + ], |
| 28 | + ... |
| 29 | +} |
| 30 | +
|
| 31 | +By 'color_data' I can easily see the things that I care about and calculate the answer |
| 32 | +1. The intial nodes in this color |
| 33 | +2. The number of node in this color |
| 34 | +
|
| 35 | +The time complexity is O(I*N), |
| 36 | +because we loop through the initial nodes. |
| 37 | +And each node, it could potential travel all the nodes. |
| 38 | +I is the initial nodes count, N is the nodes count. |
| 39 | +
|
| 40 | +Space complexity is O(N), because we use colored to keep track of all the nodes. |
| 41 | +""" |
| 42 | + |
| 43 | + |
| 44 | +class Solution(object): |
| 45 | + def minMalwareSpread(self, graph, initial): |
| 46 | + colored = set() |
| 47 | + initial_set = set(initial) |
| 48 | + color_data = {} #[4] |
| 49 | + color = 0 |
| 50 | + |
| 51 | + def dfs(node, c): |
| 52 | + stack = [node] |
| 53 | + while stack: |
| 54 | + n = stack.pop() |
| 55 | + if n in colored: continue #[1] |
| 56 | + colored.add(n) |
| 57 | + |
| 58 | + if n in initial_set: |
| 59 | + color_data[c][0].append(n) |
| 60 | + color_data[c][1]+=1 |
| 61 | + |
| 62 | + for nb in xrange(len(graph)): |
| 63 | + if graph[n][nb]==1: |
| 64 | + stack.append(nb) |
| 65 | + |
| 66 | + # [0] |
| 67 | + for node in initial: |
| 68 | + if color not in color_data: |
| 69 | + color_data[color] = [[], 0] |
| 70 | + |
| 71 | + dfs(node, color) |
| 72 | + color+=1 |
| 73 | + |
| 74 | + ans = min(initial) |
| 75 | + max_infected = float('-inf') |
| 76 | + for c in color_data.keys(): |
| 77 | + if len(color_data[c][0])!=1: continue #[3] |
| 78 | + n = color_data[c][0][0] |
| 79 | + infected = color_data[c][1] |
| 80 | + |
| 81 | + if color_data[c][1]>max_infected: #[2] |
| 82 | + max_infected = infected |
| 83 | + ans = n |
| 84 | + elif infected==max_infected and n<ans: |
| 85 | + ans = n |
| 86 | + |
| 87 | + return ans |
| 88 | + |
0 commit comments