-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0582-kill-process.js
More file actions
38 lines (31 loc) · 974 Bytes
/
0582-kill-process.js
File metadata and controls
38 lines (31 loc) · 974 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Kill Process
* Time Complexity: O(N)
* Space Complexity: O(N)
*/
var killProcess = function (pid, ppid, kill) {
const childrenMap = new Map();
pid.forEach((childIdValue, iterationIndex) => {
const parentIdValue = ppid[iterationIndex];
if (!childrenMap.has(parentIdValue)) {
childrenMap.set(parentIdValue, []);
}
childrenMap.get(parentIdValue).push(childIdValue);
});
const killedProcessesList = [];
const processIdsToVisit = [kill];
killedProcessesList.push(kill);
let queuePointer = 0;
while (queuePointer < processIdsToVisit.length) {
const currentKillingNode = processIdsToVisit[queuePointer];
queuePointer++;
const immediateChildren = childrenMap.get(currentKillingNode);
if (immediateChildren) {
immediateChildren.forEach((singleChildId) => {
killedProcessesList.push(singleChildId);
processIdsToVisit.push(singleChildId);
});
}
}
return killedProcessesList;
};