582. Kill Process

2020/02/11 Leetcode

582. Kill Process

Tags: ‘Tree’, ‘Queue’

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.

Solution

方法1: Build Tree + DFS. O(n) time O(n) space

class Solution {
    // using a HashMap or a TreeNode. Dont use both
    public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
        // build mapping
        Map<Integer, Node> map = new HashMap<>();
        for (int id: pid) { // creating a node for every PID
            Node node = new Node(id);
            map.put(id, node);
        }
        
        // build tree
        for (int i = 0; i < ppid.size(); i++) {
            if (ppid.get(i) <= 0) continue;
            
            Node parent = map.get(ppid.get(i));
            parent.children.add(map.get(pid.get(i)));
        }
        
        List<Integer> result = new ArrayList<>();
        result.add(kill);
        killAllChildren(map.get(kill), result);
        return result;
        
    }
    
    private void killAllChildren(Node root, List<Integer> result) {
        if (root == null || root.children.size() == 0) return;
        for (Node n: root.children) {
            result.add(n.val);
            killAllChildren(n, result);
        }
    }
    
    class Node {
        Integer val;
        List<Node> children;
        
        Node(int val) {
            this.val = val;
            this.children = new ArrayList<>();
        }
    }
}

方法2: HashMap + DFS. HashMap存储parent - children 信息,recursive add to result list

Creating HashMap takes O(n), killing children takes O(n)

class Solution {
    // using a HashMap or a TreeNode. Dont use both
    public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
        // build mapping
        Map<Integer, List<Integer>> map = new HashMap<>();
        for (int i = 0; i < ppid.size(); i++) {
            Integer parent = ppid.get(i);
            Integer child = pid.get(i);
            if (map.get(parent) == null) {
                List<Integer> children = new ArrayList<>();
                children.add(child);
                map.put(parent, children);
            } else {
                List<Integer> children = map.get(parent);
                children.add(child);
            }
        }
        
        List<Integer> result = new ArrayList<>();
        result.add(kill);
        killAllChildren(map, result, kill);
        return result;
    }
    
    private void killAllChildren(Map<Integer, List<Integer>> map, List<Integer> result, int kill) {
        if (!map.containsKey(kill)) return; // end case
        
        
        for (int pid: map.get(kill)) {
            result.add(pid);
            killAllChildren(map, result, pid);
        }
    }
    
}

方法3: 上述方法里面,kill 可以用 BFS + Queue O(N) time O(N) space. If input is large, this is a better option since it’s level traversal.

List<Integer> result = new ArrayList<>();
Queue<Integer> queue = new LinkedList<>();
queue.add(kill);
while (!queue.isEmpty) {
  int curr = queue.remove();
  result.add(curr);
  if (map.containsKey(curr)) {
    for (int child: map.get(curr)) {
      queue.add(child);
    }
  }
}

Search

    Table of Contents