Skip to content

Commit b32d120

Browse files
committed
Add solution #928
1 parent 387c66d commit b32d120

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,7 @@
737737
925|[Long Pressed Name](./solutions/0925-long-pressed-name.js)|Easy|
738738
926|[Flip String to Monotone Increasing](./solutions/0926-flip-string-to-monotone-increasing.js)|Medium|
739739
927|[Three Equal Parts](./solutions/0927-three-equal-parts.js)|Hard|
740+
928|[Minimize Malware Spread II](./solutions/0928-minimize-malware-spread-ii.js)|Hard|
740741
929|[Unique Email Addresses](./solutions/0929-unique-email-addresses.js)|Easy|
741742
933|[Number of Recent Calls](./solutions/0933-number-of-recent-calls.js)|Easy|
742743
937|[Reorder Data in Log Files](./solutions/0937-reorder-data-in-log-files.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* 928. Minimize Malware Spread II
3+
* https://leetcode.com/problems/minimize-malware-spread-ii/
4+
* Difficulty: Hard
5+
*
6+
* You are given a network of n nodes represented as an n x n adjacency matrix graph, where the
7+
* ith node is directly connected to the jth node if graph[i][j] == 1.
8+
*
9+
* Some nodes initial are initially infected by malware. Whenever two nodes are directly connected,
10+
* and at least one of those two nodes is infected by malware, both nodes will be infected by
11+
* malware. This spread of malware will continue until no more nodes can be infected in this manner.
12+
*
13+
* Suppose M(initial) is the final number of nodes infected with malware in the entire network after
14+
* the spread of malware stops.
15+
*
16+
* We will remove exactly one node from initial, completely removing it and any connections from
17+
* this node to any other node.
18+
*
19+
* Return the node that, if removed, would minimize M(initial). If multiple nodes could be removed
20+
* to minimize M(initial), return such a node with the smallest index.
21+
*/
22+
23+
/**
24+
* @param {number[][]} graph
25+
* @param {number[]} initial
26+
* @return {number}
27+
*/
28+
var minMalwareSpread = function(graph, initial) {
29+
const n = graph.length;
30+
const initialSet = new Set(initial);
31+
32+
initial.sort((a, b) => a - b);
33+
34+
const infected = new Set(initial);
35+
const queue = [...initial];
36+
37+
for (let i = 0; i < queue.length; i++) {
38+
const node = queue[i];
39+
for (let neighbor = 0; neighbor < n; neighbor++) {
40+
if (graph[node][neighbor] === 1 && !infected.has(neighbor)) {
41+
infected.add(neighbor);
42+
queue.push(neighbor);
43+
}
44+
}
45+
}
46+
47+
const sourcesMap = new Array(n).fill().map(() => []);
48+
49+
for (const initialNode of initial) {
50+
const reachable = new Set();
51+
const visited = new Set(initial);
52+
visited.delete(initialNode);
53+
54+
const q = [initialNode];
55+
while (q.length > 0) {
56+
const node = q.shift();
57+
reachable.add(node);
58+
59+
for (let neighbor = 0; neighbor < n; neighbor++) {
60+
if (graph[node][neighbor] === 1 && !visited.has(neighbor)) {
61+
visited.add(neighbor);
62+
q.push(neighbor);
63+
}
64+
}
65+
}
66+
67+
for (let node = 0; node < n; node++) {
68+
if (reachable.has(node) && !initialSet.has(node)) {
69+
sourcesMap[node].push(initialNode);
70+
}
71+
}
72+
}
73+
74+
const savedCounts = new Map();
75+
for (let node = 0; node < n; node++) {
76+
if (sourcesMap[node].length === 1) {
77+
const source = sourcesMap[node][0];
78+
savedCounts.set(source, (savedCounts.get(source) || 0) + 1);
79+
}
80+
}
81+
82+
let maxSaved = 0;
83+
let result = initial[0];
84+
85+
for (const node of initial) {
86+
const saved = savedCounts.get(node) || 0;
87+
if (saved > maxSaved) {
88+
maxSaved = saved;
89+
result = node;
90+
} else if (saved === maxSaved && node < result) {
91+
result = node;
92+
}
93+
}
94+
95+
return result;
96+
};

0 commit comments

Comments
 (0)