582. Kill Process

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:

Input: 
pid =  [1, 3, 10, 5, 12, 13]
ppid = [3, 0, 5, 3, 5, 12]
kill = 5
Output: [5,10]
Explanation: 
           3
         /   \
        1     5
             /   \
            10   12
                  \
                    13
Kill  5will also kill 10, 12, 13.
Note:
The given kill id is guaranteed to be one of the given PIDs.
n >= 1.

思路

  1. 由于pid和paid都一一对应关系,所以可以将其转换成HashMap来表示其关系,例题中的关系图表示如下:
key: value  (PPID, PID)
0: 3
3: 1, 5
5: 10,12
12: 13

如果要kill 5,那么除了5本身外,5对应的10,12要kill,12下面的13要kill。很自然想到BFS。

  1. 用BFS方法,不断找指定要kill的元素下面得pid。找到后放入queue,再找其下面的pid,一直找到queue 为空,则找到了所有需要kill的pid
class Solution {
    public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
        if (pid == null || ppid == null || pid.size() != ppid.size()) return null;
        
        //1. Hashmap to store the ppid and pid relationship
        HashMap<Integer, List<Integer>> mapping = new HashMap<>();
        
        for(int i = 0; i < ppid.size(); i++) {
            mapping.put(ppid.get(i), new ArrayList<Integer>());
        }
        
        for(int i = 0; i < ppid.size(); i++) {
            mapping.get(ppid.get(i)).add(pid.get(i));
        }
        
        //2. 用BFS在hashmap中找,已知的要kill的process下面都有哪些pid,然后把这些pid再次加入到queue中再找其下面的pid,直到全部找完     
        List<Integer> result = new ArrayList<Integer>();
        Queue<Integer> queue = new LinkedList<Integer>();
        queue.add(kill);
        
        while (!queue.isEmpty()) {
            int curNode = queue.poll();
            result.add(curNode);
            
            if (mapping.get(curNode) != null) {
                List<Integer> temp = mapping.get(curNode);
                for (int i = 0; i < temp.size(); i++) {
                    queue.add(temp.get(i));
                }
            }
        }
        
        return result;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 前言:对于一个后台管理系统而言,可视化展现数据是必不可少的一部分,而将这些数据导出为Excel可打开的文件的需求可...
    yaya520阅读 5,155评论 1 1
  • 如果要说功利心,我不能说自己没有。从开始跑步到现在,彼时的初衷和当下的心态已经不一样了。虽然作为一个业余跑者...
    云紫烟阅读 2,642评论 1 0
  • 千金之子坐不垂堂,百金之子不骑衡,圣主不乘危而徼幸。 ——出自史记 在职场工作一两年后,我们手里多多少少会有一些余...
    f4cd239da133阅读 3,526评论 0 7
  • 我不要天上的星星,我要尘世的幸福, 我不要世间一切美好的东西,我只要你。
    关于七罪阅读 1,315评论 0 0

友情链接更多精彩内容