C++ 176ms Very Simple and Concise Solution


  • 0
    class Solution {
    public:
        vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
            unordered_map<int, vector<int>> childrens;
            for(int i=0; i<pid.size(); ++i){
                childrens[ppid[i]].push_back(pid[i]);
            }
            
            vector<int> killed{kill};
            killed.reserve(pid.size());
            int k = 0;
            while(k < killed.size()){
                // find the children pid
                auto iter = childrens.find(killed[k++]);
                if(iter != childrens.end()){
                    std::move(iter->second.begin(), iter->second.end(), std::back_inserter(killed));
                }
            }
            return killed;
        }
    };
    

Log in to reply
 

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