forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_582.java
34 lines (30 loc) · 1.02 KB
/
_582.java
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
package com.fishercoder.solutions;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class _582 {
public static class Solution1 {
public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
Map<Integer, List<Integer>> map = new HashMap<>();
for (int i = 0; i < pid.size(); i++) {
map.putIfAbsent(ppid.get(i), new LinkedList<>());
map.get(ppid.get(i)).add(pid.get(i));
}
List<Integer> result = new LinkedList<>();
Deque<Integer> stack = new ArrayDeque<>();
stack.offer(kill);
while (!stack.isEmpty()) {
int curr = stack.poll();
result.add(curr);
List<Integer> list = map.get(curr);
if (list != null) {
stack.addAll(list);
}
}
return result;
}
}
}