Java BFS version beat 96%


  • 0
    A
    public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
        List<Integer> res = new ArrayList<Integer>(); 
        Map<Integer,List<Integer>> adj = new HashMap<Integer, List<Integer>>();
        for(int i=0; i<ppid.size(); i++){
            if(!adj.containsKey(ppid.get(i))) adj.put(ppid.get(i), new ArrayList<Integer>());
            adj.get(ppid.get(i)).add(pid.get(i));
        }
        Queue<Integer> qu = new LinkedList<Integer>(); qu.offer(kill);
        while(!qu.isEmpty()){
            int size = qu.size();
            for(int i=0; i<size; i++){
                int id = qu.poll(); res.add(id);
                if(!adj.containsKey(id))continue;
                for(int val : adj.get(id)) qu.offer(val);                
            }
        }
        return res;       
    }

Log in to reply
 

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