原题链接在这里: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. 面试:做过sql优化吗?

    近来面试找工作经常会遇见这种问题: 做过数据库优化吗?大数据量基础过吗?系统反应慢怎么查询? 这咱也没背过啊,面试还老问,现在的网站主要的压力都来自于数据库,频繁的数据库访问经常会使系统瘫痪,这样就需 ...

  2. 0927—MySQL常用语句集合

    一.连接MySQL 格式: mysql -h 主机地址 -u 用户名 -p 用户密码 1.例1:连接到本机上的MYSQL. 首先在打开DOS窗口,然后进入目录 mysql bin,再键入命令mysql ...

  3. Eclipse使用Maven搭建Java Web项目并直接部署Tomcat

    1.环境: Windows 10 Java 1.8 Maven 3.3.9 Eclipse IDE for Java EE Developers 2.准备: eclipse环境什么的不赘述,Maven ...

  4. React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发

    React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发   2016/09/23 |  React Native技术文章 |  Sky丶清|  4 条评论 |  1 ...

  5. 安装专业版的linux的方法 图解安装专业版的linux

    按装一个linux系统其实很简单,不再像以前那样光光盘就好些个,一不小心还又可能装错,实在也是一个大问题.现在好了基本上都是简单安装+网络升级先安装主要的后面如果需要什么在装什么?大大简化了流程.不行 ...

  6. (补充一)CountDownLatch

    引言:  在学习单例模式时候,用到了锁synchronized的概念,在多线程中又用到了CountDownLatch的概念 CountDownLatch是一个同步工具类,它允许一个或多个线程一直等待, ...

  7. 企业微信小程序--从零开始(带你见证从头开始的企业小程序之开发运营)

    1.注册微信小程序账户(自己摸索吧很简单的) 2.微信小程序认证 3.遇到的问题 1)

  8. LeetCode——Reverse String

    LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...

  9. Linux系统官网下载

    CentOS-6.9-x86_64-bin-DVD1.isohttp://archive.kernel.org/centos-vault/6.9/isos/x86_64/CentOS-6.9-x86_ ...

  10. 介绍几个C#正则表达式工具

    这里将为大家推荐介绍几个C#正则表达式工具,这些小工具能帮助大家在.NET开发过程中起到事半功倍的效果,希望大家喜欢. 推荐三个C#正则表达式工具,理由如下 第一个C#正则表达式工具,REGEX 这个 ...