hash + bfs solution[c++]


  • 0
    X
    class Solution {
    public:
        vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
            unordered_map<int,vector<int>> hash;
            for(int i=0;i<pid.size();i++){
                hash[ppid[i]].push_back(pid[i]);
            }
            vector<int> r;
            queue<int> q;
            q.push(kill);
            while(!q.empty()){
                auto cur = q.front();q.pop();
                r.push_back(cur);
                for(auto& c:hash[cur])q.push(c);
            }
            return r;
        }
    };
    

Log in to reply
 

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