-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
30 lines (27 loc) · 857 Bytes
/
Copy pathSolution.java
File metadata and controls
30 lines (27 loc) · 857 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
import java.util.*;
public class Solution
{
public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill)
{
Map<Integer, List<Integer>> mapping = new HashMap<>();
for (int i = 0; i < pid.size(); i++)
{
int parentProcessId = ppid.get(i);
mapping.putIfAbsent(parentProcessId, new ArrayList<>());
mapping.get(parentProcessId).add(pid.get(i));
}
List<Integer> killed = new ArrayList<>();
Queue<Integer> queue = new LinkedList<>();
queue.add(kill);
while (!queue.isEmpty())
{
int processId = queue.poll();
killed.add(processId);
if (mapping.containsKey(processId))
{
queue.addAll(mapping.get(processId));
}
}
return killed;
}
}