[c++] 159 ms - multimap solution


  • 0
    class Solution {
    public:
        vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
            unordered_multimap<int, int> children;
            for (int i = 0; i < pid.size(); ++i) {
                children.insert(make_pair<int, int>((int)ppid[i], (int)pid[i]));
            }
            
            vector<int> result;
            killProcess(children, kill, result);
            return result;
        }
        
        void killProcess(unordered_multimap<int, int>& children, int kill, vector<int>& result) {
            result.push_back(kill);
            for (auto itor = children.find(kill); itor != children.end() && itor->first == kill; ++itor) {
                killProcess(children, itor->second, result);
            }
        }
    };
    

Log in to reply
 

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