原题链接在这里:https://leetcode.com/problems/kill-process/description/

题目:

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.

题解:

通过存pid 和 ppid 的两个list 建立起 parent 和 它对应children list的map.

然后用DFS 或 BFS 从给出的process id走起就好.

Time Complexity: O(n). n = pid.size().

Space: O(n).

AC Java:

 class Solution {
public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
HashMap<Integer, List<Integer>> hm = new HashMap<Integer, List<Integer>>();
for(int i = 0; i<ppid.size(); i++){
int parent = ppid.get(i);
if(parent > 0){
List<Integer> item = hm.getOrDefault(parent, new ArrayList<Integer>());
item.add(pid.get(i));
hm.put(parent, item);
}
} List<Integer> res = new ArrayList<Integer>();
LinkedList<Integer> que = new LinkedList<Integer>();
que.add(kill);
while(!que.isEmpty()){
int cur = que.poll();
res.add(cur);
if(hm.containsKey(cur)){
que.addAll(hm.get(cur));
}
} return res;
}
}

LeetCode Kill Process的更多相关文章

  1. [LeetCode] Kill Process 结束进程

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

  2. linux 终端报错 Out of memory: Kill process[PID] [process name] score问题分析

    从Out of memory来看是内存超出了,后面的 Kill process[PID] [process name] score好像和进程有关了,下面我们就一起来看看linux 终端报错 Out o ...

  3. Kill Process by Name

    Kill Process by Name(works in: Microsoft Windows 95/98/ME/NT/2000/XP)It is sometimes necessary to te ...

  4. Mongodb副本集--Out of memory: Kill process 37325 (mongod)

    1.Mongodb副本集--Out of memory: Kill process 37325 (mongod) 场景描述: 恢复一个22TB数据的mongodb实例的时候. 将备用结点加入mongo ...

  5. 理解和配置Out of memory: Kill process

    转自:爱开源 理解 OOM killer 最近有位 VPS 客户抱怨 MySQL 无缘无故挂掉,还有位客户抱怨 VPS 经常死机,登陆到终端看了一下,都是常见的 Out of memory 问题.这通 ...

  6. Out of memory: Kill process 内存不足

    服务直接被 killed,感觉特别奇怪.代码肯定是没有问题的,但为什么放到服务器上就出错了呢. 部署时报错如下: Failed to add the deployment content to the ...

  7. Android Kill Process

    /********************************************************************** * Android Kill Process * 说明: ...

  8. Out of memory: Kill process 6033 (mysqld) score 85 or sacrifice child

    进入正题前先说明:OOM killer什么时候出现? linux下允许程序申请比系统可用内存更多的内存,这个特性叫Overcommit.这样做是出于优化系统考虑,因为不是所有的程序申请了内存就立刻使用 ...

  9. [LeetCode] 582. Kill Process 终止进程

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

随机推荐

  1. 手动漏洞挖掘-SQL注入(安全牛笔记)

    substring_index(USER(),"@",l)-- #是将查询出来的结果进行切分,以@符号的方式切分 ’ union select table_name,table_s ...

  2. 限制goroutine数量写法

    虽然golang的goroutine可以开启无数个goroutine,但是没有限制也是不行的.我就写一下我对goroutine数量限制的写法 1.初始化goroutine协程池.把goroutine数 ...

  3. 配置Nginx反向代理服务器

    一.主要配置文件:/etc/nginx/nginx.conf 内容如下图 扩展配置文件:/etc/nginx/conf.d/*.conf 图中的主配置文件的末尾,加载所有扩展配置文件里面以.conf结 ...

  4. orecle常用函数

    Oracle SQL 提供了用于执行特定操作的专用函数.这些函数大大增强了 SQL 语言的功能.函数可以接受零个或者多个输入参数,并返回一个输出结果. oracle 数据库中主要使用两种类型的函数 1 ...

  5. LeetCode——Keyboard Row

    LeetCode--Keyboard Row Question Given a List of words, return the words that can be typed using lett ...

  6. Struts2小例子

    第一个Struts 2.0例子 工具:MyEclipse 6.0.1 第一步:新建web project 第二步:为项目加入Struts 2.0 的jar包 官方下载地址:http://struts. ...

  7. python递归列出目录及其子目录下所有文件

    python递归列出目录及其子目录下所有文件 一.前言 函数的递归,简单来说,就是函数内部调用自己 先举个小例子,求阶乘 def factorial(n): if n == 0: return 1 e ...

  8. yii2:属性

    yii2:属性 在 PHP 中,类的成员变量也被称为属性(properties). 以Foo.php类文件为例: 继承yii\base\Object类(基于类内的 getter 和 setter(读取 ...

  9. 如何批量更改linux文件的内容

    在工作当中,我们往往需要修改某个文件夹下面所有文件的内容,例如把里面的日期统一替换成新的日期,或者把某一串字符替换成另外一串字符,这时我们就可以使用sed命令: sed -i "s/olds ...

  10. [6644] 02 Apr 23:11:58.976 # Creating Server TCP listening socket *:6379: bind: No such file or directory

    redis报错: [6644] 02 Apr 23:11:58.976 # Creating Server TCP listening socket *:6379: bind: No such fil ...