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.这样做是出于优化系统考虑,因为不是所有的程序申请了内存就立刻使用 ...
随机推荐
- 为HttpClient和HttpURLConnection添加中国移动代理
转自: http://www.2cto.com/kf/201111/112100.html 在android中,一般需要联网的时候前,都要做一次网络的判断,判断当前的网络状态!然后开始请求网络 当我们 ...
- 可变类型的安全性——更锋利的C#代码小记(2)
ReadOnlyCollection类型是.NET系统类库提供的一个只读集合类型,它与原来的List<string>不存在任何类型转换关系,因此可以从根本上阻止外部对其的修改操作using ...
- js promise 介绍和使用
1.什么是promise js是单线程执行的. ajax是典型的异步操作,我们通常会在ajax的成功或者失败之后写上回掉函数.这中写法是一种嵌套的方式,如果回掉多了会造成代码复杂并且难以复用. pro ...
- 用RecyclerView做一个小清新的Gallery效果
一.简介 RecyclerView现在已经是越来越强大,且不说已经被大家用到滚瓜烂熟的代替ListView的基础功能,现在RecyclerView还可以取代ViewPager实现Banner效果,当然 ...
- 用 dojo/request/script 玩垮域
dojo/request/script 可以用于向服务器发送跨域请求,如JSONP等.但单看官方文档有点不容易理解,特将体会记录. require(["dojo/request/script ...
- Node.js——require加载规则
判断require中的标识参数: 非路径的标识参数:也被称为是核心模块,已经被编译到二进制文件中 带有路径标识参数:自定义模块,一般都是相对定位 第三方模块:表现形式与核心模块一样,但是实际不一样,它 ...
- iOS截取特定的字符串(正则匹配)
有时候我们会有需求从一个字符串中截取其他的字符串,根据情况的不同,我们来分析几种方法~~ 一. 固定长度字符串中截取固定位置长度的字符串 // 这是比较简单的一种情况:比如截取手机号的后4位 let ...
- CAD参数绘制直线(com接口)
用户可以在CAD控件视区任意位置绘制直线. 主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下: 参数 说明 DOUBLE dX1 直线的开始点x坐标 DOUBLE ...
- 梦想CAD控件,用于浏览和编辑DWG文件,在脱离AUTOCAD的情况下独立运行,相当于简易CAD
(百度百科连接) 梦想绘图控件5.2 是国内最强,最专业的CAD开发组件(控件),不需要AutoCAD就能独立运行.控件使用VC 2010开发,最早从2007年第一个版本完成,经过多年的累积已经非常 ...
- E. String Multiplication
E. String Multiplication time limit per test 2 seconds memory limit per test 256 megabytes input sta ...