[LeetCode] 582. Kill Process

You have n processes forming a rooted tree structure. You are given two integer arrays pid and ppid, where pid[i] is the ID of the ith process and ppid[i] is the ID of the ith process's parent process.

Each process has only one parent process but may have multiple children processes. Only one process has ppid[i] = 0, which means this process has no parent process (the root of the tree).

When a process is killed, all of its children processes will also be killed.

Given an integer kill representing the ID of a process you want to kill, return a list of the IDs of the processes that will be killed. You may return the answer in any order.

Example 1:

Input: pid = [1,3,10,5], ppid = [3,0,5,3], kill = 5
Output: [5,10]
Explanation: The processes colored in red are the processes that should be killed.

Example 2:

Input: pid = [1], ppid = [0], kill = 1
Output: [1]

Constraints:

  • n == pid.length
  • n == ppid.length
  • 1 <= n <= 5 * 104
  • 1 <= pid[i] <= 5 * 104
  • 0 <= ppid[i] <= 5 * 104
  • Only one process has no parent.
  • All the values of pid are unique.
  • kill is guaranteed to be in pid.

杀死进程。

题意是给两个 list,一个叫做 pid,表示一堆进程的 id;一个叫做 ppid,表示一堆父进程的 id。同时给一个参数 kill 表示一个需要被结束的进程的 id。请问如果结束了 kill 进程,按照进程和父进程的关系,所有被杀死的进程都是哪些,用 list 输出。

这道题有 BFS 和 DFS 两种思路,两种做法的时间空间复杂度都是O(n)。这道题目给了 pid 和 ppid 两个数组,因为每个进程 process 只能有一个父进程,但是一个父进程有可能是有多个子进程的,所以这是一个类似多叉树的问题。这道题跟其他遍历类型的题不太一样的地方在于无论是 BFS 还是 DFS,首先都需要用一个数据结构创建/记录进程之间的父子关系。这里用 hashmap 相对比较合适。hashmap里是 <parentID, a list of childrenIDs>。

BFS

BFS的做法,既然一开始给了 kill 进程,所以在邻接关系创建好之后,就可以把 kill 进程放入 queue。从 queue 中弹出的时候,再把这个 kill 进程当做父进程,去找到他的所有子进程,放入 queue。

 1 class Solution {
 2     public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
 3         HashMap<Integer, List<Integer>> map = new HashMap<>();
 4         // 对于每一个进程
 5         for (int i = 0; i < pid.size(); i++) {
 6             // 找到他的父进程
 7             int parent = ppid.get(i);
 8             // 创建邻接表
 9             // <parentId, list of childIDs>
10             map.putIfAbsent(parent, new ArrayList<>());
11             map.get(parent).add(pid.get(i));
12         }
13 
14         List<Integer> res = new ArrayList<>();
15         Queue<Integer> queue = new LinkedList<>();
16         queue.offer(kill);
17         while (!queue.isEmpty()) {
18             int cur = queue.poll();
19             res.add(cur);
20             queue.addAll(map.getOrDefault(cur, new ArrayList<>()));
21         }
22         return res;
23     }
24 }

 

DFS

 1 class Solution {
 2     public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
 3         // 建立邻接表
 4         HashMap<Integer, List<Integer>> map = new HashMap<>();
 5         for (int i = 0; i < pid.size(); i++) {
 6             int parent = ppid.get(i);
 7             map.putIfAbsent(parent, new ArrayList<>());
 8             map.get(parent).add(pid.get(i));
 9         }
10 
11         List<Integer> res = new ArrayList<>();
12         res.add(kill);
13         helper(map, res, kill);
14         return res;
15     }
16 
17     private void helper(HashMap<Integer, List<Integer>> map, List<Integer> res, int kill) {
18         if (map.containsKey(kill)) {
19             for (int p : map.get(kill)) {
20                 res.add(p);
21                 helper(map, res, p);
22             }
23         }
24     }
25 }

 

LeetCode 题目总结

posted @ 2020-08-26 05:46  CNoodle  阅读(558)  评论(0编辑  收藏  举报