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. linux 服务器配置 ASF 云挂卡

    关于社区打不开:https://github.com/zyfworks/AnotherSteamCommunityFix 下载asf:https://github.com/JustArchi/Arch ...

  2. CodeForces - 83D:Numbers (数学&递归 - min25筛 )

    pro:给定三个整数L,R,P求[L,R]区间的整数有多少个是以P为最小因子的.L,R,P<2e9; sol: 一: 比较快的做法是,用函数的思想递归. 用solve(N,P)表示求1到N有多少 ...

  3. 实用Golang库

    框架: 1. Golang轻量级并发服务器框架: zinx / https://www.jianshu.com/p/23d07c0a28e52. 国内谢大牛模仿django制作的重框架: beego3 ...

  4. wordpress调用自定义菜单

    wordpress要调用自定义菜单首先要注册菜单,将代码添加到主题文件夹下的function.php中,比如wordpress自带主题2019的定义如下 // This theme uses wp_n ...

  5. python的numpy.array

    为什么要用numpy Python中提供了list容器,可以当作数组使用.但列表中的元素可以是任何对象,因此列表中保存的是对象的指针,这样一来,为了保存一个简单的列表[1,2,3].就需要三个指针和三 ...

  6. freemarker使用shiro标签(spring boot)

    freemarker使用shiro标签(spring boot) 2018年07月03日 14:20:37 niu_sayok 阅读数:348更多 个人分类: freeMarkerShiro   首先 ...

  7. eclipse 配置python环境 json 插件

    windows->install new software add 配置python 环境: name:pydev(可随意写) url:http://pydev.org/updates/ (如果 ...

  8. php 正则达达示中的模式修正符

    我们通过元字符和原子完成了正则表达示的入门.有一些特殊情况我们依然需要来处理.深圳dd马达 如果abc在第二行的开始处如何匹配?我不希望正则表达示特别贪婪的匹配全部,只匹配一部份怎么办? 这个时候,我 ...

  9. [后端]gitlab之gitlab-ci自动部署

    转发:https://www.jianshu.com/p/df433633816b 简介 gitlab-ci全称是gitlab continuous integration的意思,也就是持续集成.中心 ...

  10. circus 做为批处理的守护进程

    circus 是集成了zeromq,使用python编写的一个进程以及socket 管理工具,使用circus 的进程管理,我们可以用来进行批任务的 处理,同时又能保证任务的准确 项目使用docker ...