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 ...
随机推荐
- SQL-ALTER-change和modify区别
ALTER 对于列的应用: 1.更改列名 格式:CHANGE old_col_name new_col_name column_definition 保留old和new列名 ...
- Easy-RSA 3快速入门自述文件
Easy-RSA 3快速入门自述文件 这是使用Easy-RSA版本3的快速入门指南.运行./easyrsa -h可以找到有关使用和特定命令的详细帮助.可以在doc /目录中找到其他文档. 如果您从Ea ...
- camera corder profile
/system/etc/ 其中的qulity high 必须与 最大的支持的分辨率相同. 不然cts 不过. 这里的配置必须在报告给app的数据匹配.
- maven说明
1.maven 仓库地址 http://mvnrepository.com/ 2.maven jar包搜索地址 http://search.maven.org/ 3. 点开上面的 版本链接,就可以看到 ...
- hadoop入门小知识点
注意各个主机之间的通信 文件的复制 scp指令 scp /etc/profile acm03:/etc 所有历史版本: archive.apache.org hdfs://acm01:9000 ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- eclipse中的错误解决——Servlet cannot be resolved to a type
问题如图 解决问题方法
- 【Boost】boost库asio详解5——resolver与endpoint使用说明
tcp::resolver一般和tcp::resolver::query结合用,通过query这个词顾名思义就知道它是用来查询socket的相应信息,一般而言我们关心socket的东东有address ...
- NextPermutation,寻找下一个全排列
问题描述:给定一个数组是一个全排列,寻找下一个全排列.例如123->132, 321->123, 115->151. 算法分析:从后往前寻找顺序,找到后从往前寻找第一个大于当前元素, ...
- TCP_AIO_Server_ZC_02
ZC: 这个例子是,1个skt 投递 N个未决的接收操作 (记得 以前查过 说 线程数是 CUP数量的2倍 比较合适) 1. // 当需要 投递多个接收操作的时候,可以将接收缓冲封装成类,然后再投递多 ...