Given n processes, each process have a unique PID (process ID) and its PPID (parent process ID).
Each process is only having one parent process, but could have one or more children processes. This was just like a tree structure. Only one process had PPID that was 0, which means this process had no parent process. All the PIDs'll be distinct positive integers.
We use both list of integers to represent a list of processes, where the first list contains PID for each process and the S Econd list contains the corresponding PPID.
Now given the both lists, and a PID representing a process you want to kill, return a list of PIDs of processes that would b E killed in the end. You should assume if a process is killed and all its children processes would be killed. No order is required for the final answer.
Example 1:
Input:pid = [1, 3, ten, 5]ppid = [3, 0, 5, 3]kill = 5Output: [5,10]explanation: 3 / 1 5 / 10Ki LL 5 'll also kill 10.
Note:
- The given kill ID is guaranteed to be one of the given PIDs.
- N >= 1.
Give two arrays, one is the process, and the other is an array of the parent processes of each process in the process array. Ends a process, all of its child processes need to be ended, and because a process may have more than one child process, the relationship between the parent and child processes is cleared.
Solution: Use a hash table to establish a mapping between the process and all its child processes. Put the process you want to end in the results, and then put all the child processes and the following processes into the results.
Python:dfs, Time:o (n), Space:o (n)
Class solution (Object): def killprocess (self, PID, ppid, Kill): "" " : Type Pid:list[int] : type ppid: List[int] : Type Kill:int : Rtype:list[int] "" def killAll (PID, children, killed): Killed.append (PID) for child in Children[pid]: killAll (child, children, killed) result = [] Children = collections.defaultdict (set) for I in Xrange (Len (PID)): Children[ppid[i]].add (Pid[i]) KillAll (Kill, children, result) return result
PYTHON:BFS, Time:o (n), Space:o (n)
Class solution (Object): def killprocess (self, PID, ppid, Kill): "" " : Type Pid:list[int] : type ppid: List[int] : Type Kill:int : Rtype:list[int] "" " result = [] children = collections.defaultdict ( Set) for I in Xrange (Len (PID)): Children[ppid[i]].add (pid[i]) q = Collections.deque () q.append ( Kill) while Q: p = q.popleft () result.append (p) to child in Children[p]: q.append (Child) return result
C++:
Class Solution {public: vector<int> killprocess (vector<int>& pid, vector<int>& Ppid, int kill) { vector<int> res; Queue<int> Q{{kill}}; Unordered_map<int, vector<int>> m; for (int i = 0; i < pid.size (); ++i) { m[ppid[i]].push_back (pid[i]); } while (!q.empty ()) { int t = Q.front (); Q.pop (); Res.push_back (t); for (int p:m[t]) { q.push (p); } } return res; }};
C++:
Class Solution {public: vector<int> killprocess (vector<int>& pid, vector<int>& Ppid, int kill) { vector<int> res; Unordered_map<int, vector<int>> m; for (int i = 0; i < pid.size (); ++i) { m[ppid[i]].push_back (pid[i]); } Helper (Kill, M, res); return res; } void helper (int kill, Unordered_map<int, vector<int>>& m, vector<int>& Res) { Res.push_ Back (kill); for (int p:m[kill]) { Helper (p, M, res);}} ;
[Leetcode] 582. Kill process to terminate processes