LeetCode:Kill Process

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.

思路:这里使用广度优先搜素就可以了,无需构建树。
 1 vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill)
 2 {
 3     vector<int>res,q1, q2;
 4     map<int, vector<int>>m;
 5     if (pid.size() == 0 || ppid.size() == 0)
 6         return res;
 7     for (int i = 0; i < ppid.size(); i++)
 8         m[ppid[i]].push_back(pid[i]);
 9     q1.push_back(kill);
10     while (q1.size())
11     {
12         for (int i = 0; i < q1.size(); i++)
13         {
14             res.push_back(q1[i]);
15             for (int j = 0; j < m[q1[i]].size(); j++)
16                 q2.push_back(m[q1[i]][j]);
17         }
18         swap(q1, q2);
19         q2.clear();
20     }
21     return res;
22 }
时间: 2024-11-15 09:35:06

LeetCode:Kill Process的相关文章

[LeetCode] 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

LeetCode 582. Kill Process 解题报告

[例子1]  Kill Process Problem statement: 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 str

out of memory kill process (java) score or a child

最近在跑大数据,发现 out of memory kill process (java) score or a child,查资料发现是操作系统linux low mem 太低,后来加大内存一样不管用,开始分析代码: 将代码中的String 字符串拼接改成StringBuilder(单线程速度比StringBuffer快) 将获取文件名称的方法file.listFiles() 改成 file.list() 将代码中的不必要的创建对象.数组开销去掉,尤其是在循环里 发现仍然有问题,通过free -

Android Kill Process

/********************************************************************** * Android Kill Process * 说明: * 在Android App中杀死其他的App进程. * * 2017-8-5 深圳 龙华樟坑村 曾剑锋 *********************************************************************/ 一.参考文档: 1. Why KILL_BACKG

Out of memory: Kill process 内存不足

服务直接被 killed,感觉特别奇怪.代码肯定是没有问题的,但为什么放到服务器上就出错了呢. 部署时报错如下: Shell代码   Failed to add the deployment content to the repository: java.util.concurrent.CancellationException: Operation was cancelled wildfly 报错如下: Shell代码   ./standalone.sh: line 307: 12526 Ki

[LeetCode] 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

582. Kill Process

Problem statement: 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 pro

How to kill process in SAP ?

方法1 > SM50 -> flag the process you want to kill -> go to PROCESS --> Cancel WITHOUT core 方法2 -->su – <SID>adm -->   dpmon pf=<profilename> -->m -->p/L -->k -->work precess number -->yes 方法3     kill -9 PID 方法4

Linux中:Out of memory: Kill process 1270 (java) score 478 or sacrifice child 或者:java.lang.OutOfMemoryError: PermGen space

解:内存不足,自动查杀了进程,即杀死了项目进程,项目不能启动 这个问题的原因是low memory耗尽."内核使用low memory来跟踪所有的内存分配,一旦low memory耗尽,就会查杀进程,以保持系统的正常运转.说白了 OOM Killer 就是一层保护机制,用于避免 Linux 在内存不足的时候不至于出太严重的问题,把无关紧要的进程杀掉 解:找到:bin/catalina.sh,在这个前面 echo "Using CATALINA_BASE:   $CATALINA_BAS