Java Solution


  • 0
    V

    public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
    ArrayList<Integer> rst = new ArrayList<Integer>();
    ArrayList<Integer> queueList = new ArrayList<Integer>();
    queueList.add(kill);

        HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
            
        for (int i = 0; i < ppid.size(); i++) {
            if (map.containsKey(ppid.get(i))) {
                map.get(ppid.get(i)).add(pid.get(i));
            } else {
                ArrayList<Integer> list = new ArrayList<Integer>();
                list.add(pid.get(i));
                map.put(ppid.get(i), list);
            }
        }
        
        
        while (queueList.size() != 0) {
            int size = queueList.size();
            
            for (int i = 0; i < size; i++) {
                int id = queueList.get(0);
                queueList.remove(0);
                rst.add(id);
                
                if (map.containsKey(id)) {
                    queueList.addAll(map.get(id));
                }
            }
        }
                
        return rst;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.