582. Kill Process
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:
- The given kill id is guaranteed to be one of the given PIDs.
- 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的更多相关文章
- 582. Kill Process杀死所有子代
[抄题]: Given n processes, each process has a unique PID (process id) and its PPID (parent process id) ...
- [LeetCode] 582. Kill Process 终止进程
Given n processes, each process has a unique PID (process id) and its PPID (parent process id). Each ...
- linux 终端报错 Out of memory: Kill process[PID] [process name] score问题分析
从Out of memory来看是内存超出了,后面的 Kill process[PID] [process name] score好像和进程有关了,下面我们就一起来看看linux 终端报错 Out o ...
- Kill Process by Name
Kill Process by Name(works in: Microsoft Windows 95/98/ME/NT/2000/XP)It is sometimes necessary to te ...
- Mongodb副本集--Out of memory: Kill process 37325 (mongod)
1.Mongodb副本集--Out of memory: Kill process 37325 (mongod) 场景描述: 恢复一个22TB数据的mongodb实例的时候. 将备用结点加入mongo ...
- 理解和配置Out of memory: Kill process
转自:爱开源 理解 OOM killer 最近有位 VPS 客户抱怨 MySQL 无缘无故挂掉,还有位客户抱怨 VPS 经常死机,登陆到终端看了一下,都是常见的 Out of memory 问题.这通 ...
- Out of memory: Kill process 内存不足
服务直接被 killed,感觉特别奇怪.代码肯定是没有问题的,但为什么放到服务器上就出错了呢. 部署时报错如下: Failed to add the deployment content to the ...
- Android Kill Process
/********************************************************************** * Android Kill Process * 说明: ...
- Out of memory: Kill process 6033 (mysqld) score 85 or sacrifice child
进入正题前先说明:OOM killer什么时候出现? linux下允许程序申请比系统可用内存更多的内存,这个特性叫Overcommit.这样做是出于优化系统考虑,因为不是所有的程序申请了内存就立刻使用 ...
随机推荐
- java使用正则表达式对注册页面进行验证
package regex; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Patt ...
- CoreText的使用方法
- (void)draw { CGContextRef context = UIGraphicsGetCurrentContext(); NSMutableAttributedString *attr ...
- jmeter(二)元件的作用域与执行顺序
1.元件的作用域 JMeter中共有8类可被执行的元件(测试计划与线程组不属于元件),这些元件中,取样器是典型的不与其它元件发生交互作用的元件,逻辑控制器只对其子节点的取样器有效,而其它元件(conf ...
- [BZOJ1257][CQOI2007]余数之和sum 数学+分块
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1257 题目所求为$$Ans=\sum_{i=1}^nk%i$$ 将其简单变形一下$$Ans ...
- Android习惯——给全部Activity添加集合管理
title: 2017-3-23 Android习惯--给全部Activity添加集合管理 public void ActivityCollector{ public static List<A ...
- Android一句代码给Activity定制标题栏
在此之前,使用过几种方法设置标题栏: 1.常规法:这个方法是最常用的了,哪个activity需要什么样的标题栏,就在对应的xml布局设计.缺点:此方法维护起来困难,没有将标题栏的共性抽取出来, 如果要 ...
- rar在linux下安装更新
1.下载:根据主机系统下载合适的版本,当前64为centos系统演示下载: wget http://www.rarlab.com/rar/rarlinux-x64-5.3.0.tar.gz 2.解压安 ...
- mysql执行语句汇总
插入select的数据 INSERT INTO `test1`( order_id, goods_id, goods_name, goods_sn, product_id, goods_number, ...
- Xcode 9 打印信息解决
Xcode 9 打印信息解决 打印信息 1 nw_proxy_resolver_create_parsed_array PAC evaluation error: kCFErrorDomainCFNe ...
- 关于SQL Server数据表的五种约束
1.主键约束(PRIMARY KEY) 主键约束可以在表中定义一个主键值,它可以唯一确定表中每一条记录,每个表中只能有一个主键约束(只能有一个主键约束的意思并不是说受主键约束的列只能有一个),并且受主 ...