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. Windows 7操作系统下PHP 7的安装与配置(图文详解)

    前提博客 Windows 7操作系统下Apache的安装与配置(图文详解) 从官网下载           PHP的官网 http://www.php.net/         特意,新建这么一个目录 ...

  2. jQuery Ajax使用实例

    <script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.js"></script> <scr ...

  3. Android使用Gson(相当于C#的Newtonsoft.Json)非常好用

    C#转Java有一段时间了,之前做ASP.NET WebAPI微软竟将第三方类库Newtonsoft.Json作为VS新建MVC和WebAPI项目默认必备的Json工具Nuget包,可想而知这个包有多 ...

  4. LN : leetcode 646 Maximum Length of Pair Chain

    lc 646 Maximum Length of Pair Chain 646 Maximum Length of Pair Chain You are given n pairs of number ...

  5. web 自动化测试 selenium基础到应用(目录)

    第一章   自动化测试前提及整体介绍 1-1功能测试和自动化测试的区别 1-2自动化测试流程有哪些 1-3自动化测试用例和手工用例的区别 1-4 自动化测试用例编写 1-5 selenium的优势以及 ...

  6. C# 方法 虚方法的调用浅谈 引用kdalan的博文

    我们在面试中经常碰到有关多态的问题,之前我也一直被此类问题所困扰,闹不清到底执行哪个方法. 先给出一道简单的面试题,大家猜猜看,输出是?     public class A    {         ...

  7. ES6语法糖集锦

    sublime3安装Es6插件 javascriptNext,然后安装即可 Java​Script​Next - ES6 Syntax()高亮插件 -------------------------- ...

  8. [Windows Server 2008] 搭建数据云备份

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:如何搭建数 ...

  9. Vue 数组和对象更新,但是页面没有刷新

    在使用数组的时候,数组内部数据发生改变,但是与数组绑定的页面的数据却没有发生变化. <ul> <li v-for="(item,index) in todos" ...

  10. uiviewcontroller顶级布局控制

    @available(iOS 7.0, *) open var edgesForExtendedLayout: UIRectEdge // Defaults to UIRectEdgeAll @ava ...