C++, unodered_map and queue solution


  • 0
    J
    class Solution {
    public:
        vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
            unordered_map<int,set<int>> map;
            for(int i = 0;i<ppid.size();i++) map[ppid[i]].insert(pid[i]);
            queue<int>q;
            q.push(kill);
            vector<int>res;
            while(!q.empty())
            {
                int front = q.front();
                res.push_back(front);
                q.pop();
                for(auto it=map[front].begin();it!=map[front].end();it++)
                    q.push(*it);
            }
            return res;
        }
    };
    

Log in to reply
 

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