872. Kill Process
中文English
In this problem, each process has a unique PID (process id)and PPID (parent process id).
Each process only has one parent process, but may have one or more children processes. This is just like a tree structure. Only one process has PPID that is 0, which means this process has no parent process. All the PIDs will be distinct positive integers.
We use two list of integers to represent a list of processes, where the first list contains PID for each process and the second list contains the corresponding PPID.
Now given the two lists, and a PID representing a process you want to kill, return a list of PIDs of processes that will be killed in the end. You should assume that when a process is killed, all its children processes will be killed. No order is required for the final answer.
Example
Example 1:
Input: PID = [1, 3, 10, 5], PPID = [3, 0, 5, 3], killID = 5
Output: [5, 10]
Explanation: Kill 5 will also kill 10.
3
/ \
1 5
/
10
Example 2:
Input: PID = [1, 2, 3], PPID = [0, 1, 1], killID = 2
Output: [2]
Notice
- The given kill id is guaranteed to be one of the given PIDs.
- There is at least one PID in the list.
Input test data (one parameter per line)How to understand a testcase?
解法1:
通过某节点找到其某祖上是kill之后,将该节点到kill之间所有点都加入result。有点象union-find的路径压缩。
代码如下:
class Solution {
public:
/**
* @param pid: the process id
* @param ppid: the parent process id
* @param kill: a PID you want to kill
* @return: a list of PIDs of processes that will be killed in the end
*/
vector<int> killProcess(vector<int> &pid, vector<int> &ppid, int kill) {
int n = pid.size();
if (n == 0) return vector<int>();
for (int i = 0; i < n; ++i) {
father[pid[i]] = ppid[i];
}
killed.insert(kill);
for (int i = 0; i < n; ++i) {
if (killed.find(pid[i]) != killed.end()) continue;
if (findParent(pid[i], ppid, kill)) {
int node = pid[i];
while(node != kill && node != 0) {
killed.insert(node);
node = father[node];
}
}
}
vector<int> result(killed.begin(), killed.end());
return result;
}
private:
bool findParent(int node, vector<int> &ppid, int target) {
if (node == target) return true;
while(node != target && node != 0) {
node = father[node];
if (node == 0) return false;
if (killed.find(node) != killed.end()) return true;
if (node == target) return true;
}
return false;
}
set<int> killed;
map<int, int> father;
};
解法2:
class Solution {
public:
/**
* @param pid: the process id
* @param ppid: the parent process id
* @param kill: a PID you want to kill
* @return: a list of PIDs of processes that will be killed in the end
*/
vector<int> killProcess(vector<int> &pid, vector<int> &ppid, int kill) {
int n = pid.size();
if (n == 0) return vector<int>();
unordered_map<int, int> father;
unordered_set<int> killed;
vector<int> result;
result.resize(0);
for (int i = 0; i < n; ++i) {
father[pid[i]] = ppid[i];
}
for (int i = 0; i < n; ++i) {
int node = pid[i];
while(node != 0) {
if (node == kill) {
result.push_back(pid[i]);
break;
}
node = father[node];
}
}
return result;
}
};
336



被折叠的 条评论
为什么被折叠?



