• [LeetCode] 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]
    ppid = [3, 0, 5, 3]
    kill = 5
    Output: [5,10]
    Explanation: 
               3
             /   
            1     5
                 /
                10
    Kill 5 will also kill 10.

    Note:

    1. The given kill id is guaranteed to be one of the given PIDs.
    2. n >= 1.

    杀死进程。

    题意是给两个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 题目总结

  • 相关阅读:
    chapter02“良/恶性乳腺癌肿瘤预测”的问题
    ASCII编码和Unicode编码的区别
    Spring AOP概述
    Spring 基于注解的配置
    Spring Bean作用域&FactoryBean
    Spring <bean> 之间的关系&整合多个配置文件
    Spring 方法注入
    Spring 简化装配Bean的配置方式
    Spring 注入参数详解
    vue-router 导航守卫
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13562975.html
Copyright © 2020-2023  润新知