http://bookshadow.com/weblog/2017/05/15/leetcode-kill-process/
题目给了我们两个数组,一个是进程的数组,还有一个是进程数组中的每个进程的父进程组成的数组。题目中说结束了某一个进程,其所有的子进程都需要结束,由于一个进程可能有多个子进程,所以我们首先要理清父子进程的关系。所以我们使用一个哈希表,建立进程和其所有子进程之间的映射,然后我们首先把要结束的进程放入一个队列queue中,然后while循环,每次取出一个进程,将其加入结果res中,然后遍历其所有子进程,将所有子进程都排入队列中,这样我们就能结束所有相关的进程
https://eugenejw.github.io/2017/07/leetcode-582
http://blog.csdn.net/u014688145/article/details/71948763
map+队列,因为PID的父类不会继承多个PPID,所以我们可以用PPID作为键值,把PPID的孩子们都找到存在Map中,KILL某个进程时,依次把它的孩子进程也杀死即可,因此存在队列中
public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) { Map<Integer,List<Integer>> map = new HashMap<>(); for (int i = 0; i < ppid.size(); i++){ map.computeIfAbsent(ppid.get(i), k -> new ArrayList<Integer>()).add(pid.get(i)); } List<Integer> ans = new ArrayList<>(); Queue<Integer> queue = new LinkedList<>(); queue.offer(kill); while (!queue.isEmpty()){ int target = queue.poll(); ans.add(target); if (map.containsKey(target)){ List<Integer> tmp = map.get(target); for (int num : tmp){ queue.offer(num); } } } return ans; }
X. DFS
X. Tree Simulation/Build Tree
X.
Given n processes, each process has a unique PID (process id) and its PPID (parent process id).
Each process only has one parent process, but may have one or more children processes. This is just like a tree structure. Only one process has PPID that is 0, which means this process has no parent process. All the PIDs will be distinct positive integers.
We use two list of integers to represent a list of processes, where the first list contains PID for each process and the second list contains the corresponding PPID.
Now given the two lists, and a PID representing a process you want to kill, return a list of PIDs of processes that will be killed in the end. You should assume that when a process is killed, all its children processes will be killed. No order is required for the final answer.
Example 1:
Note:
- The given kill id is guaranteed to be one of the given PIDs.
- n >= 1.
题目给了我们两个数组,一个是进程的数组,还有一个是进程数组中的每个进程的父进程组成的数组。题目中说结束了某一个进程,其所有的子进程都需要结束,由于一个进程可能有多个子进程,所以我们首先要理清父子进程的关系。所以我们使用一个哈希表,建立进程和其所有子进程之间的映射,然后我们首先把要结束的进程放入一个队列queue中,然后while循环,每次取出一个进程,将其加入结果res中,然后遍历其所有子进程,将所有子进程都排入队列中,这样我们就能结束所有相关的进程
https://eugenejw.github.io/2017/07/leetcode-582
http://blog.csdn.net/u014688145/article/details/71948763
map+队列,因为PID的父类不会继承多个PPID,所以我们可以用PPID作为键值,把PPID的孩子们都找到存在Map中,KILL某个进程时,依次把它的孩子进程也杀死即可,因此存在队列中
public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) { Map<Integer,List<Integer>> map = new HashMap<>(); for (int i = 0; i < ppid.size(); i++){ map.computeIfAbsent(ppid.get(i), k -> new ArrayList<Integer>()).add(pid.get(i)); } List<Integer> ans = new ArrayList<>(); Queue<Integer> queue = new LinkedList<>(); queue.offer(kill); while (!queue.isEmpty()){ int target = queue.poll(); ans.add(target); if (map.containsKey(target)){ List<Integer> tmp = map.get(target); for (int num : tmp){ queue.offer(num); } } } return ans; }
树的层次遍历
利用孩子表示法建立进程树
然后从被杀死的进程号开始,执行层次遍历。
vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) { vector<int> res; unordered_map<int, vector<int>> m; for (int i = 0; i < pid.size(); ++i) { m[ppid[i]].push_back(pid[i]); } helper(kill, m, res); return res; } void helper(int kill, unordered_map<int, vector<int>>& m, vector<int>& res) { res.push_back(kill); for (int p : m[kill]) { helper(p, m, res); } }
public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
HashMap<Integer, List<Integer>> map = new HashMap<>();
for (int i = 0; i < ppid.size(); i++) {
if (ppid.get(i) > 0) {
List<Integer> l = map.getOrDefault(ppid.get(i), new ArrayList<Integer>());
l.add(pid.get(i));
map.put(ppid.get(i), l);
}
}
List<Integer> l = new ArrayList<>();
l.add(kill);
getAllChildren(map, l, kill);
return l;
}
public void getAllChildren(HashMap<Integer, List<Integer>> map, List<Integer> l, int kill) {
if (map.containsKey(kill))
for (int id : map.get(kill)) {
l.add(id);
getAllChildren(map, l, id);
}
}
X. Tree Simulation/Build Tree
class Node {
int val;
List<Node> children = new ArrayList<>();
}
public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
HashMap<Integer, Node> map = new HashMap<>();
for (int id : pid) {
Node node = new Node();
node.val = id;
map.put(id, node);
}
for (int i = 0; i < ppid.size(); i++) {
if (ppid.get(i) > 0) {
Node par = map.get(ppid.get(i));
par.children.add(map.get(pid.get(i)));
}
}
List<Integer> l = new ArrayList<>();
l.add(kill);
getAllChildren(map.get(kill), l);
return l;
}
public void getAllChildren(Node pn, List<Integer> l) {
for (Node n : pn.children) {
l.add(n.val);
getAllChildren(n, l);
}
}
X.
- Time complexity : . function calls will be made in the worst case
- Space complexity : . The depth of the recursion tree can go upto .
public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
List<Integer> l = new ArrayList<>();
if (kill == 0)
return l;
l.add(kill);
for (int i = 0; i < ppid.size(); i++)
if (ppid.get(i) == kill)
l.addAll(killProcess(pid, ppid, pid.get(i)));
return l;
}