Open azl397985856 opened 1 week ago
class Solution { public int minMalwareSpread(int[][] graph, int[] initial) { Arrays.sort(initial); int N = graph.length; int ans = initial[0]; int max = 0; boolean[] init = new boolean[N]; for (int p : initial) { init[p] = true; } for (int p : initial) { init[p] = false; int count = process(graph, p, new boolean[N], init); if (count > max) { max = count; ans = p; } init[p] = true; } return ans; }
private int process(int[][] graph, int p, boolean[] visited, boolean[] initial) {
if (initial[p]) {
return 0;
}
visited[p] = true;
int count = 1;
for (int q = 0; q < graph[p].length; q++) {
if (!visited[q] && graph[p][q] == 1) {
int c = process(graph, q, visited, initial);
if (c == 0) {
return 0;
}
count += c;
}
}
return count;
}
}
924. 尽量减少恶意软件的传播
入选理由
暂无
题目地址
https://leetcode-cn.com/problems/minimize-malware-spread
前置知识
暂无
题目描述