LeetCode Kill Process
原题链接在这里: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:
- The given kill id is guaranteed to be one of the given PIDs.
- 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的更多相关文章
- [LeetCode] Kill Process 结束进程
Given n processes, each process has a unique PID (process id) and its PPID (parent process id). Each ...
- linux 终端报错 Out of memory: Kill process[PID] [process name] score问题分析
从Out of memory来看是内存超出了,后面的 Kill process[PID] [process name] score好像和进程有关了,下面我们就一起来看看linux 终端报错 Out o ...
- Kill Process by Name
Kill Process by Name(works in: Microsoft Windows 95/98/ME/NT/2000/XP)It is sometimes necessary to te ...
- Mongodb副本集--Out of memory: Kill process 37325 (mongod)
1.Mongodb副本集--Out of memory: Kill process 37325 (mongod) 场景描述: 恢复一个22TB数据的mongodb实例的时候. 将备用结点加入mongo ...
- 理解和配置Out of memory: Kill process
转自:爱开源 理解 OOM killer 最近有位 VPS 客户抱怨 MySQL 无缘无故挂掉,还有位客户抱怨 VPS 经常死机,登陆到终端看了一下,都是常见的 Out of memory 问题.这通 ...
- Out of memory: Kill process 内存不足
服务直接被 killed,感觉特别奇怪.代码肯定是没有问题的,但为什么放到服务器上就出错了呢. 部署时报错如下: Failed to add the deployment content to the ...
- Android Kill Process
/********************************************************************** * Android Kill Process * 说明: ...
- Out of memory: Kill process 6033 (mysqld) score 85 or sacrifice child
进入正题前先说明:OOM killer什么时候出现? linux下允许程序申请比系统可用内存更多的内存,这个特性叫Overcommit.这样做是出于优化系统考虑,因为不是所有的程序申请了内存就立刻使用 ...
- [LeetCode] 582. Kill Process 终止进程
Given n processes, each process has a unique PID (process id) and its PPID (parent process id). Each ...
随机推荐
- 在Windows Server 2008 R2上打开ping的方法
默认安装完Windows Server 2008 R2后,从外面ping服务器的地址是ping不通的,原因是服务器防火墙默认关闭了ICMP的回显请求.需要按照如下方法打开: 在服务器管理器中选择“配置 ...
- 20145201 《Java程序设计》第一周学习总结
# 20145201 <Java程序设计>第一周学习总结 ## 教材学习内容总结 万事开头难,终于开始学习了Java.寒假的时候看到老师的要求确实有点慌,但是这周翻开书,从书本知识第一行学 ...
- 利用web workers实现多线程处理
利用web workers在后台线程中实现对数据库的增删改查操作,并在后台线程中生成页面上某个列表的完整的HTML代码,然后再前台脚本中直接将这段HTML代码输出到页面上! 利用web workers ...
- ubuntu16.04的anacoda内置的spyder不支持中文【学习笔记】
执行下面的语句:将libfcitxplatforminputcontextplugin.so复制到anaconda2的安装目录下的platforminputcontexts目录重启生效 cp /usr ...
- PHP的date函数的时区问题
来自:http://www.cnblogs.com/fuland/p/4250462.html(“腐烂的翅膀”的博客) 从php5.1.0开始,php.ini里加入了date.timezone这个选项 ...
- JAVA基础补漏--反射
获得CLASS的三种方式: 1.Class.forname("全类名"):将字节码文件加载进内存,返回Class对象. 多用于配置文件,将类名放到配置文件中,读取配置文件,加载类 ...
- 互联网开放平台API安全设计
互联网开放平台设计1.需求:现在A公司与B公司进行合作,B公司需要调用A公司开放的外网接口获取数据,如何保证外网开放接口的安全性.2.常用解决办法:2.1 使用加签名方式,防止篡改数据2.2 使用Ht ...
- Largest Rectangle in Histogram, 求矩形图中最大的长方形面积
问题描述: Given n non-negative integers representing the histogram's bar height where the width of each ...
- Java条件语句之 if
生活中,我们经常需要先做判断,然后才决定是否要做某件事情.例如,如果考试成绩大于 90 分,则奖励一个 IPHONE 5S .对于这种“需要先判断条件,条件满足后才执行的情况”,就可以使用if 条件语 ...
- Windows系统变量列表
%ALLUSERSPROFILE% : 列出所有用户Profile文件位置. %APPDATA% : 列出应用程序数据的默认存放位置. %CD% : 列出当前目录. %CLIENTNAME% : ...