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. 208 Implement Trie (Prefix Tree) 字典树(前缀树)

    实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个方法.注意:你可以假设所有的输入都是小写字母 a-z.详见:https://leetcode.co ...

  2. Anaconda(miniconda)安装及使用--转

    https://www.waitalone.cn/anaconda-install-error.html         3,224   1.Anaconda概述 Anaconda是一个用于科学计算的 ...

  3. .Net应用自定义鼠标样式

    (调用系统API的方法) 1.引用命名空间 using System.Runtime.InteropServices; 命名空间提供各种各样支持 COM 互操作 及平台调用服务的成员.using Sy ...

  4. Java8特性之Lambda、方法引用以及Stream流

    Java 8 中的 Streams API 详解:https://www.ibm.com/developerworks/cn/java/j-lo-java8streamapi/ Java笔记——Jav ...

  5. [ SDOI 2010 ] 古代猪文

    \(\\\) Description 一句话题意: 设 \(x=\sum_{d|n} C_n^d\),求 \(G^x\pmod {999911659}\) . 从原题面大段语文中其实不难推出所求. \ ...

  6. iOS Programming Localization 本地化

    iOS Programming Localization 本地化 Internationalization is making sure your native cultural informatio ...

  7. day1 python 基础

    # 一行注释"""多行注释"""print("hello world\n" * 3)name = "sure& ...

  8. CreateWindowEx详解

    语法: HWND CreateWindowEx( DWORD dwExStyle, LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, ...

  9. 二维码之zxing仿新浪微博二维码

    在前言中最后部分,提到了二维码开发工具资源ZXing.网上有它最新1.7版的源码,感兴趣的可以下载下来看看,要打包生成core比较麻烦,网上有相关教程.嫌麻烦的朋友,可以去我的资源里下载Java版的c ...

  10. ajax 分页点击数据缓存

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...