Java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**

 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:
 The given kill id is guaranteed to be one of the given PIDs.
 n >= 1.

 */

public class Kill_Process {

    public class Solution {

        public List < Integer > killProcess(List < Integer > pid, List < Integer > ppid, int kill) {
            List< Integer > result = new ArrayList< >();
            if (kill == 0) {
                return result;
            }

            result.add(kill);
            for (int i = 0; i < ppid.size(); i++)
                if (ppid.get(i) == kill)
                    result.addAll(killProcess(pid, ppid, pid.get(i)));
            return result;
        }
    }

    public class Solution_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 > result = new ArrayList < > ();
            result.add(kill);
            getAllChildren(map.get(kill), result);
            return result;
        }
        public void getAllChildren(Node pn, List < Integer > l) {
            for (Node n: pn.children) {
                l.add(n.val);
                getAllChildren(n, l);
            }
        }
    }

    public class Solution_dfsWithCache {
        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 > result = new ArrayList < > ();
            result.add(kill);
            getAllChildren(map, result, kill);
            return result;
        }
        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);
                }
        }
    }


}

Java

class Solution {
    public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
        Map<Integer, List<Integer>> parentChildrenMap = new HashMap<Integer, List<Integer>>();
        int size = pid.size();
        for (int i = 0; i < size; i++) {
            int parentId = ppid.get(i), childId = pid.get(i);
            List<Integer> children = parentChildrenMap.getOrDefault(parentId, new ArrayList<Integer>());
            children.add(childId);
            parentChildrenMap.put(parentId, children);
        }
        List<Integer> killedPid = new ArrayList<Integer>();
        Queue<Integer> queue = new LinkedList<Integer>();
        queue.offer(kill);
        while (!queue.isEmpty()) {
            int curId = queue.poll();
            killedPid.add(curId);
            List<Integer> children = parentChildrenMap.getOrDefault(curId, new ArrayList<Integer>());
            for (int child : children)
                queue.offer(child);
        }
        return killedPid;
    }
}

All Problems

All Solutions