[抄题]:

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了,不能每次都忘记。

[一句话思路]:

  1. 先把主要的pid都添加一遍,最后再一起dfs
  2. 题目已经制定了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杀死所有子代的更多相关文章

  1. 582. Kill Process

    Problem statement: Given n processes, each process has a unique PID (process id) and its PPID (paren ...

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

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

  3. Android Kill Process

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

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

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

  5. Kill Process by Name

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

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

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

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

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

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

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

  9. 【linux】kill ;杀死某一用户下的所有进程

    [linux]kill :杀死某一用户下的所有进程 https://my.oschina.net/u/347414/blog/600854

随机推荐

  1. Mathematics for Computer Science (Eric Lehman / F Thomson Leighton / Albert R Meyer 著)

    I Proofs1 What is a Proof?2 The Well Ordering Principle3 Logical Formulas4 Mathematical Data Types5 ...

  2. 支付宝 生活号 获取 userId 和 生活号支付

    第一:申请生活号. 第二:激活开发者 模式 ,并且上创 自己的 公钥  ( 支付宝 demo 里面有 ) 第三: 配置 回调地址 ( 用于前端 调用获取 auth_code 的时候 填写的回调地址,支 ...

  3. Eclipse Java EE IDE for Web Developers集成的Maven 3 指向自己安装的 Maven

    一.配置Maven环境 1.下载apache-maven文件,选择自己需要的版本,地址:Apache 官方下载地址是http://maven.apache.org/download.cgi 2.下载并 ...

  4. centos 7 su jenkins 切换不过去

    root切换到jenkins用户: passwd jenkins:设置jenkins用户密码 su jenkins : 切换不过去, 查看passwd文件 cat /etc/passwd 找到:jen ...

  5. 关于STL容器

    容器: 概念:如果把数据看做物体,容器就是放置这些物体的器物,因为其内部结构不同,数据摆放的方式不同,取用的方式也不同,我们把他们抽象成不同的模板类,使用时去实例化它 分类: 序列容器.关联容器.容器 ...

  6. Windows下好用的git客户端--GitExtentions

    用git: https://git-scm.com/downloads GitExtentions: https://sourceforge.net/projects/gitextensions/ B ...

  7. WINDOWS NT操作系统的注册表文件

    WINDOWS NT操作系统的注册表文件 WINDOWS NT注册表文件分为系统文件和用户文件两类. 系统设置和缺少用户 配置数据存放在系统C:\Windows\System32\config文件夹下 ...

  8. LDAP认证模式简介

    今天发现公共服务中有ldap数据库服务,先大概了解一下ldap,转载下面的文章.原文链接:https://www.jianshu.com/p/d3f8c8f5d661 另外记录一篇文章地址:https ...

  9. 一切为了解决隐私问题,绿洲实验室Ekiden协议介绍

    绿洲实验室官网截图 下一代区块链平台的竞争已经悄然展开,每个月我们都能看到新成立的创业公司宣称,他们要采用区块链解决所有问题.大约80-90%的区块链项目,运行在像Ethereum这样的平台上. 创建 ...

  10. 知识点:Mysql 基本用法之存储过程

    存储过程 一. 介绍 存储过程包含了一系列可执行的sql语句,存储过程存放于MySQL中,通过调用它的名字可以执行其内部的一堆sql 使用存储过程的优点: 用于替代程序写的SQL语句,实现程序与sql ...