Problem statement:

Given n processes, each process has a unique PID (process id) and its 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 1:

Input:
pid = [1, 3, 10, 5]
ppid = [3, 0, 5, 3]
kill = 5
Output: [5,10]
Explanation:
3
/ \
1 5
/
10
Kill 5 will also kill 10.

Note:

  1. The given kill id is guaranteed to be one of the given PIDs.
  2. n >= 1.

Solution one: TLE

This is the second question of weekly contest 32. It a very good question, tests your basic computer knowledge and programming skills. It comes from real computer world since the process hierarchy is well-known by every computer scientist.

After extracting the information, it is a BFS model. We can naively search the ppid to get its direct children, and no doubt, it is TLE. For each element, we need O(n) time to search its direct children which is at mot the number of n, time complexity will get as high as O(n ^ n), the space complexity is O(1). But, I stupidly solve the problem like this and report a TLE bug.

class Solution {
public:
vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
vector<int> proc;
queue<int> que;
que.push(kill);
while(!que.empty()){
int pp_id = que.front();
proc.push_back(pp_id);
que.pop();
for(int i = ; i < ppid.size(); i++){
if(ppid[i] == pp_id){
que.push(pid[i]);
}
}
}
return proc;
}
};

Solution two: BFS + unordered_map(AC)

We need to optimize the solution one. One preprocessing step is to build a hash table indexed by process ID, and get the direct children set by this index. Each time, the time complexity to get the direct children of a process is O(1), the total time complexity is O(n). The space complexity is O(n) as there is a hash table to store the children information of each process.

class Solution {
public:
vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
unordered_map<int, vector<int>> hash; // <pid, children_set>
// build hash table which indexed by process ID
for(int i = ; i < ppid.size(); i++){
hash[ppid[i]].push_back(pid[i]);
}
// bfs queue
queue<int> que;
que.push(kill);
vector<int> proc;
// bfs loop
while(!que.empty()){
int pp_id = que.front();
que.pop();
proc.push_back(pp_id);
for(int i = ; i < hash[pp_id].size(); i++){
que.push(hash[pp_id][i]);
}
}
return proc;
}
};

Solution three: DFS + unordered_map(AC)

Same with solution two, this approach employs DFS to search the process.

class Solution {
public:
vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
unordered_map<int, vector<int>> hash_table; // <pid, children_set>
// build hash table which indexed by process ID
for(int i = ; i < ppid.size(); i++){
hash_table[ppid[i]].push_back(pid[i]);
}
vector<int> proc;
proc.push_back(kill);
find_process_dfs(proc, hash_table, kill);
return proc;
}
private:
//dfs search
void find_process_dfs(vector<int>& proc, unordered_map<int, vector<int>>& hash_table, int pid){
for(auto process : hash_table[pid]){
proc.push_back(process);
find_process_dfs(proc, hash_table, process);
}
return;
}
};

582. Kill Process的更多相关文章

  1. 582. Kill Process杀死所有子代

    [抄题]: Given n processes, each process has a unique PID (process id) and its PPID (parent process id) ...

  2. [LeetCode] 582. Kill Process 终止进程

    Given n processes, each process has a unique PID (process id) and its PPID (parent process id). Each ...

  3. linux 终端报错 Out of memory: Kill process[PID] [process name] score问题分析

    从Out of memory来看是内存超出了,后面的 Kill process[PID] [process name] score好像和进程有关了,下面我们就一起来看看linux 终端报错 Out o ...

  4. Kill Process by Name

    Kill Process by Name(works in: Microsoft Windows 95/98/ME/NT/2000/XP)It is sometimes necessary to te ...

  5. Mongodb副本集--Out of memory: Kill process 37325 (mongod)

    1.Mongodb副本集--Out of memory: Kill process 37325 (mongod) 场景描述: 恢复一个22TB数据的mongodb实例的时候. 将备用结点加入mongo ...

  6. 理解和配置Out of memory: Kill process

    转自:爱开源 理解 OOM killer 最近有位 VPS 客户抱怨 MySQL 无缘无故挂掉,还有位客户抱怨 VPS 经常死机,登陆到终端看了一下,都是常见的 Out of memory 问题.这通 ...

  7. Out of memory: Kill process 内存不足

    服务直接被 killed,感觉特别奇怪.代码肯定是没有问题的,但为什么放到服务器上就出错了呢. 部署时报错如下: Failed to add the deployment content to the ...

  8. Android Kill Process

    /********************************************************************** * Android Kill Process * 说明: ...

  9. Out of memory: Kill process 6033 (mysqld) score 85 or sacrifice child

    进入正题前先说明:OOM killer什么时候出现? linux下允许程序申请比系统可用内存更多的内存,这个特性叫Overcommit.这样做是出于优化系统考虑,因为不是所有的程序申请了内存就立刻使用 ...

随机推荐

  1. Kruskal算法 分类: c/c++ 算法 2014-10-01 17:09 540人阅读 评论(0) 收藏

    Kruskal算法计算最小生成树,只与边有关,时间复杂度O(eloge) 步骤: 1.将边按权值递增排序 2.依次取出边加入最小生成树中并保证无环,判断是否成环可利用并查集. 例:http://ac. ...

  2. Zygote和System进程的启动过程、Android应用进程启动过程

    1.基本过程 init脚本的启动Zygote Zygote进程的启动 System进程的启动 Android应用进程启动过程 2.init脚本的启动 +------------+ +-------+ ...

  3. Dock

    搭建本地 Registry - 每天5分钟玩转 Docker 容器技术(20) 小结: dock 版本号 分为 3位,比如1.1.2 就分为1, 1.1,1.1,2 这个几个版本 这种 tag 方案使 ...

  4. 自己制作ssl证书

    首先执行如下命令生成一个key  openssl genrsa -des3 -out ssl.key 1024  然后他会要求你输入这个key文件的密码.不推荐输入.因为以后要给nginx使用.每次r ...

  5. 转】Spark SQL 之 DataFrame

    原博文出自于: http://www.cnblogs.com/BYRans/p/5003029.html 感谢! Spark SQL 之 DataFrame 转载请注明出处:http://www.cn ...

  6. Oracle函数大全下载

    Oracle函数大全下载 是一个压缩包,里面是一个chm格式的帮助文档,很实用.

  7. JDK11源码分析之集合类(一)----HashMap

    一,首先需要拉取JDK11源码: 方便起见我给出芋道源码作者已经拉取好的openJDK11的GitHub地址只需要fork一下克隆到本地导入IDEA中就可以对源码分析了: https://github ...

  8. vue-router: $router.push遇到的问题

    如下图使用path时,跳转路由后的页面this.$route.params为undefined 改为name时,this.$route.params可以正确获取数据,如下图: 此为官方定义的固定搭配: ...

  9. 《基于Node.js实现简易聊天室系列之环境搭建》

    前文提到了Demo所涉及的技术,现在讲环境(工具)的配置.环境的配置主要是数据库mongDB和Node.js的配置. Node.js Node.js的官方地址:https://nodejs.org/e ...

  10. java调用jacob生成pdf,word,excel横向

    /* * 传进一个office文件的byte[]以及后缀,生成一个pdf文件的byte[] */ public byte[] jacob_Office2Pdf(byte[] srcFileBytes, ...