C++ solution 175ms, beats 100%


  • 1
    S

    Similar to Java Solution using HashMap.
    vector instead of stack/queue.

    class Solution {
    public:
        vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
            unordered_map<int,vector<int>> mMap;
            for(int i = 0;i<ppid.size();++i) {
                mMap[ppid[i]].push_back(pid[i]);
            }
            vector<int> retVec;
            vector<int> mVec;
            mVec.emplace_back(kill);
            while(!mVec.empty()){
                auto val = *(mVec.end()-1);
                retVec.emplace_back(val);
                mVec.pop_back();
                if(mMap.find(val)!=mMap.end()) {
                    mVec.insert(mVec.begin(),mMap[val].begin(),mMap[val].end());
                }
            }
            return retVec;
        }
    };
    

Log in to reply
 

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