原题链接在这里: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. LVS管理工具--ipvsadm

    一. ipvsadm工具介绍 从2.4版本开始,linux内核默认支持LVS.要使用LVS的能力,只需安装一个LVS的管理工具:ipvsadm. LVS的结构主要分为两部分: 工作在内核空间的IPVS ...

  2. Boot-Repair&usb_repair

    https://help.ubuntu.com/community/Boot-Repair https://askubuntu.com/questions/500647/unable-to-mount ...

  3. shell编程学习笔记之标准输入输出(read&echo)

    2017-07-17 09:32:07 输入read: 用途: 从标准输入读取一行,或者从文件描述符FD(file descriptor)中读取一行,并且将其分割成字段. 用法: read [-ers ...

  4. MyBatis无限输出日志

    最近在项目中使用mybatis与spring集成,由于项目使用maven分模块打包,经常遇到mybatis mapper少配置子模块或者maven pom中忘记引用子模块导致的mybatis加载不到d ...

  5. “玲珑杯”ACM比赛 Round #13 B -- 我也不是B(二分排序)

    题意:开始有一个空序列s,一个变量c=0,接着从左往右依次将数组a中的数字放入s的尾部,每放一个数字就检测一次混乱度K,当混乱度k大于M时就清空序列并让c=c+1 K = Bi * Vi(1<= ...

  6. Apache HTTP Server——官网下载

    Windows版 Apache 2.4.x OpenSSL 1.0.2 VC14  ——Apache 2.4.34 x64(注:x64就是64位,x86就是32位) https://www.apach ...

  7. 有谁知道什么工具测试IOS手机上APP的性能软件啊?

    有谁知道什么工具测试IOS手机上APP的性能软件啊?

  8. 读取和修改xml

    如有一个xml文件DownData.xml,内容如下 <?xml version="1.0" standalone="yes"?> <Root ...

  9. CodeChef CHEFSOC2 Chef and Big Soccer 水dp

    Chef and Big Soccer   Problem code: CHEFSOC2 Tweet     ALL SUBMISSIONS All submissions for this prob ...

  10. Java中sleep()与wait()的区别

    第一种解释: 功能差不多,都用来进行线程控制,他们最大本质的区别是:sleep()不释放同步锁,wait()释放同步缩.       还有用法的上的不同是:sleep(milliseconds)可以用 ...