[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 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.

思路:

用一个map去对应父节点与它的所有子节点,map<int,set<int>>。子节点放到set里面。

然后用深度优先遍历的方法去遍历。DFS。

 void killCore(map<int, set<int>>& nodes, vector<int>& ret, int kill)
     {
         ret.push_back(kill);
         //set<int>::iterator it;
         //for (it = nodes[kill].begin(); it != nodes[kill].end();it++)
         //{
            // killCore(nodes, ret, *it);
         //}
         for (int child : nodes[kill])
         {
             killCore(nodes, ret, child);
         }
     }
     vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill)
     {
         vector<int> ret;
         map<int,set<int>> nodes;//每一个父结点对应的子结点
         for (int i = 0; i < pid.size();i++)
         {
             nodes[ppid[i]].insert(pid[i]);
         }
         killCore(nodes, ret, kill);

         return ret;
     }

参考:

https://leetcode.com/problems/kill-process/#/solutions

时间: 2024-08-26 19:22:34

[leetcode-582-Kill Process]的相关文章

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

[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

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 proc

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

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