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.

给两个数组,一个是进程,一个是进程数组中的每个进程的父进程组成的数组。结束了某一个进程,其所有的子进程都需要结束,由于一个进程可能有多个子进程,所以要理清父子进程的关系。

解法:使用一个哈希表,建立进程和其所有子进程之间的映射。先把把要结束的进程放入结果中,然后将所有子进程以及以下的进程放入结果中。

Python: DFS, Time: O(n), Space: O(n)

class Solution(object):
def killProcess(self, pid, ppid, kill):
"""
:type pid: List[int]
:type ppid: List[int]
:type kill: int
:rtype: List[int]
"""
def killAll(pid, children, killed):
killed.append(pid)
for child in children[pid]:
killAll(child, children, killed) result = []
children = collections.defaultdict(set)
for i in xrange(len(pid)):
children[ppid[i]].add(pid[i])
killAll(kill, children, result)
return result

Python: BFS, Time: O(n), Space: O(n)

class Solution(object):
def killProcess(self, pid, ppid, kill):
"""
:type pid: List[int]
:type ppid: List[int]
:type kill: int
:rtype: List[int]
"""
result = []
children = collections.defaultdict(set)
for i in xrange(len(pid)):
children[ppid[i]].add(pid[i])
q = collections.deque()
q.append(kill)
while q:
p = q.popleft()
result.append(p)
for child in children[p]:
q.append(child)
return result  

C++:

class Solution {
public:
vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
vector<int> res;
queue<int> q{{kill}};
unordered_map<int, vector<int>> m;
for (int i = 0; i < pid.size(); ++i) {
m[ppid[i]].push_back(pid[i]);
}
while (!q.empty()) {
int t = q.front(); q.pop();
res.push_back(t);
for (int p : m[t]) {
q.push(p);
}
}
return res;
}
};

C++:

class Solution {
public:
vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
vector<int> res;
unordered_map<int, vector<int>> m;
for (int i = 0; i < pid.size(); ++i) {
m[ppid[i]].push_back(pid[i]);
}
helper(kill, m, res);
return res;
}
void helper(int kill, unordered_map<int, vector<int>>& m, vector<int>& res) {
res.push_back(kill);
for (int p : m[kill]) {
helper(p, m, res);
}
}
};

  

  

All LeetCode Questions List 题目汇总

[LeetCode] 582. Kill Process 终止进程的更多相关文章

  1. Linux常用指令---kill | killall(终止进程)

    kill Linux中的kill命令用来终止指定的进程(terminate a process)的运行,是Linux下进程管理的常用命令.通常,终止一个前台进程可以使用Ctrl+C键,但是,对于一个后 ...

  2. Linux用ps命令查找进程PID再用kill命令终止进程的方法

    使用linux操作系统,难免遇到一些软件"卡壳"的问题,这时就需要使用linux下强大的kill命令来结束相关进程.这在linux系统下是极其容易的事情,你只需要kill xxx即 ...

  3. [LeetCode] Kill Process 结束进程

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

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

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

  5. 使用kill命令终止进程shell脚本

    因有的程序使用kill才能结束掉进程,没有关闭脚本,以我司的服务为例,服务名叫asset-server服务,只有启动脚本,自编写关闭脚本,及重启动脚本. 关闭服务脚本. vim asset-shutd ...

  6. 582. Kill Process

    Problem statement: Given n processes, each process has a unique PID (process id) and its PPID (paren ...

  7. kill 根据PID终止进程

    根据PID终止进程 kill [option] PID-list kill 通过向一个或多个进程发送信号来终止进程.除超级用户外,只有进程的所有者才可以对进程执行kill 参数 PID-list为ki ...

  8. Linux查看进程和终止进程的技巧

    1. 在LINUX命令平台输入1-2个字符后按Tab键会自动补全后面的部分(前提是要有这个东西,例如在装了tomcat的前提下,输入tomcat的to按tab). 2. ps 命令用于查看当前正在运行 ...

  9. Linux查看命令终止进程

    Linux查看命令终止进程 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ ps PID TTY TIME CMD 2576 pts/0 00:00:00 ba ...

随机推荐

  1. 如何让VS像CB一样使用

    之前用VS,先是完成了GLUT库下的opengl使用: 然后得知GLUT有些过时,又按照教程接触了GLFW库下,反正对我来说是有些复杂. 今天正式试一试用VS来写ACM的题目,发现不能定义string ...

  2. UVA 13024: Saint John Festival(凸包+二分 ,判定多个点在凸包内)

    题意:给定N个点,Q次询问,问当前点知否在N个点组成的凸包内. 思路:由于是凸包,我们可以利用二分求解. 二分思路1:求得上凸包和下凸包,那么两次二分,如果点在对应上凸包的下面,对应下凸包的上面,那么 ...

  3. Oracle中INSTR函数与SQL Server中CHARINDEX函数

    Oracle中INSTR函数与SQL Server中CHARINDEX函数 1.ORACLE中的INSTR INSTR函数格式:INSTR(源字符串, 目标字符串, 起始位置, 匹配序号) 说明:返回 ...

  4. JAVA添加WORD文档批注

    本文将介绍在Java程序中如何给Word文档中的指定字符串添加批注.前文中,主要介绍的是针对某个段落来添加批注,以及回复.编辑.删除批注的方法,如果需要针对特定关键词或指定字符串来设置批注,可以参考本 ...

  5. Bootstrap内栅格布局,表格,按钮,图片的个人总结

    栅格布局: container,固定宽度的容器. container-fluid,百分百宽度的容器. 使用行(row)在水平方向上创建一组列(colmun). 每一行中最多能够包含12列,超出的列则另 ...

  6. A - Happy Birthday, Polycarp!

    Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4) A. Happy Birthday, Polyc ...

  7. learning scasl notes

    接收类型参数的类和特质是“泛型”的,但是它们生成的类型是"参数化". ”泛型“的意思是我们用一个泛化的类或特质来定义许许多多具体的类型. 如果说S是类型T的子类型,那么Queue[ ...

  8. 关于异常System.ArgumentException

    什么是System.ArgumentException 当向方法提供的参数之一无效时引发的异常. 继承 Object Exception SystemException ArgumentExcepti ...

  9. 计蒜之道 百度AI小课堂-上升子序列

    计蒜之道 百度AI小课堂-上升子序列 题目描述 给一个长度为 \(n\) 的数组 \(a\) .试将其划分为两个严格上升子序列,并使其长度差最小. 输入格式 输入包含多组数据. 数据的第一行为一个正整 ...

  10. 使用jstack命令查看CPU高占用的问题记录

    笔记: 1.top命令找出最高占用的进程(command为java) 2.查看高负载进程下的高负载线程:top -Hp [PID] (或 ps -mp PID -o THREAD,tid,time) ...