582. Kill Process杀死所有子代
[抄题]:
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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
kill = 0时,pid直接返回
[思维问题]:
以为要从中序构建树。但是构建出来之后还是需要dfs,来找出所有子代。
[英文数据结构或算法,为什么不用别的数据结构或算法]:
一对多的数据结构,就要用hashmap了,不能每次都忘记。
[一句话思路]:
- 先把主要的pid都添加一遍,最后再一起dfs
- 题目已经制定了pid是unique的,所以用list即可,不需要set
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
构建出来之后还是需要dfs,来找出所有子代。
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
//initialization: map
List<Integer> result = new ArrayList<Integer>();
HashMap<Integer, Set<Integer>> map = new HashMap<>();
int n = pid.size();
//corner case
if (kill == 0) return pid;
//put all the pid and ppid into the map
for (int i = 0; i < n; i++) {
//if no key, add new set
if (map.get(ppid.get(i)) == null)
map.put(ppid.get(i), new HashSet<Integer>());
//add value
map.get(ppid.get(i)).add(pid.get(i));
}
//dfs
dfs(kill, map, result);
//return
return result;
}
public void dfs(int pid, HashMap<Integer, Set<Integer>> map, List<Integer> result) {
//add pid to result
result.add(pid);
//exit when !get(pid)
if (!map.containsKey(pid)) return ;
//expand to child
Set<Integer> children = map.get(pid);
for (int child : children) {
dfs(child, map, result);
}
}
}
582. Kill Process杀死所有子代的更多相关文章
- 582. Kill Process
Problem statement: Given n processes, each process has a unique PID (process id) and its PPID (paren ...
- [LeetCode] 582. Kill Process 终止进程
Given n processes, each process has a unique PID (process id) and its PPID (parent process id). Each ...
- Android Kill Process
/********************************************************************** * Android Kill Process * 说明: ...
- 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 ...
- 【linux】kill ;杀死某一用户下的所有进程
[linux]kill :杀死某一用户下的所有进程 https://my.oschina.net/u/347414/blog/600854
随机推荐
- POJ1738 An old Stone Game
题意 Language:Default An old Stone Game Time Limit: 5000MS Memory Limit: 30000K Total Submissions: 439 ...
- PythonStudy——Pycharm 小技巧
分享Pycharm中一些不为人知的技巧 工欲善其事必先利其器,Pycharm 是最受欢迎的Python开发工具,它提供的功能非常强大,是构建大型项目的理想工具之一,如果能挖掘出里面实用技巧,能带来事半 ...
- openstry lua redis实现负载均衡
需求: 通过URI地址http://10.0.0.148/test2?uuid=123的uuid参数值的第一位,去实现redis的负载均衡 若uuid第一位为1,那么去10.0.0.148的redis ...
- Vue 表单校验 vee-validate
gitHub 地址:https://github.com/baianat/vee-validate 官网API 地址:https://baianat.github.io/vee-validate/ap ...
- npm cnpm
npm 1.说明: npm(node package manager)是nodejs的包管理器,用于node插件管理(包括安装.卸载.管理依赖等) 2.使用npm安装插件:命令提示符执行npm ins ...
- django的forms认证组件
django的forms认证组件 每个网站的注册界面都需要有相应的"认证"功能,比如说认证注册页面的用户名是否已被注册,二次输入的密码是否一致以及认证用户输入的用户名.邮箱.手机号 ...
- word embeddding和keras中的embedding
训练好的词向量模型被保存下来,该模型的本质就是一个m*n的矩阵,m代表训练语料中词的个数,n代表训练时我们设定的词向量维度.当我们训练好模型后再次调用时,就可以从该模型中直接获取到对应词的词向量. 通 ...
- linux服务之apache篇(一)
1.apache介绍:使用率最高的网站服务器: URL:统一资源定位符: 端口:http:80 https:443 2.apache三种工作模式: prefork:一个线程处理一个请求(占用内存多 ...
- django之 使用views.py里面的函数对表进行增删改查 内容(models.py中表的创建、views.py中函数的使用,基于对象的跨表查询)
models之对于表的创建有以下几种: 一对一:ForeignKey("Author",unique=True), OneToOneField("Author" ...
- Maven私服(Nexus)启动创建Windows服务
sc create lutao-maven-nexus binpath= D:\maven-server\nexus--\bin\nexus.exe type= own start= auto dis ...