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

  1. The given kill id is guaranteed to be one of the given PIDs.
  2. n >= 1.

给两个数组,一个是进程,一个是进程数组中的每个进程的父进程组成的数组。结束了某一个进程,其所有的子进程都需要结束,由于一个进程可能有多个子进程,所以要理清父子进程的关系。

解法:使用一个哈希表,建立进程和其所有子进程之间的映射。先把把要结束的进程放入结果中,然后将所有子进程以及以下的进程放入结果中。

Python: DFS, Time: O(n), Space: O(n)

class Solution(object):
    def killProcess(self, pid, ppid, kill):
        """
        :type pid: List[int]
        :type ppid: List[int]
        :type kill: int
        :rtype: List[int]
        """
        def killAll(pid, children, killed):
            killed.append(pid)
            for child in children[pid]:
                killAll(child, children, killed)

        result = []
        children = collections.defaultdict(set)
        for i in xrange(len(pid)):
            children[ppid[i]].add(pid[i])
        killAll(kill, children, result)
        return result

Python: BFS, Time: O(n), Space: O(n)

class Solution(object):
    def killProcess(self, pid, ppid, kill):
        """
        :type pid: List[int]
        :type ppid: List[int]
        :type kill: int
        :rtype: List[int]
        """
        result = []
        children = collections.defaultdict(set)
        for i in xrange(len(pid)):
            children[ppid[i]].add(pid[i])
        q = collections.deque()
        q.append(kill)
        while q:
            p = q.popleft()
            result.append(p)
            for child in children[p]:
                q.append(child)
        return result  

C++:

class Solution {
public:
    vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
        vector<int> res;
        queue<int> q{{kill}};
        unordered_map<int, vector<int>> m;
        for (int i = 0; i < pid.size(); ++i) {
            m[ppid[i]].push_back(pid[i]);
        }
        while (!q.empty()) {
            int t = q.front(); q.pop();
            res.push_back(t);
            for (int p : m[t]) {
                q.push(p);
            }
        }
        return res;
    }
};

C++:

class Solution {
public:
    vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
        vector<int> res;
        unordered_map<int, vector<int>> m;
        for (int i = 0; i < pid.size(); ++i) {
            m[ppid[i]].push_back(pid[i]);
        }
        helper(kill, m, res);
        return res;
    }
    void helper(int kill, unordered_map<int, vector<int>>& m, vector<int>& res) {
        res.push_back(kill);
        for (int p : m[kill]) {
            helper(p, m, res);
        }
    }
};

  

  

原文地址:https://www.cnblogs.com/lightwindy/p/9532814.html

时间: 2024-10-25 06:34:45

[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

Linux常用指令---kill | killall(终止进程)

kill Linux中的kill命令用来终止指定的进程(terminate a process)的运行,是Linux下进程管理的常用命令.通常,终止一个前台进程可以使用Ctrl+C键,但是,对于一个后台进程就须用kill命令来终止,我们就需要先使用ps/pidof/pstree/top等工具获取进程PID,然后使用kill命令来杀掉该进程.kill命令是通过向进程发送指定的信号来结束相应进程的.在默认情况下,采用编号为15的TERM信号.TERM信号将终止所有不能捕获该信号的进程.对于那些可以捕

Linux命令:kill命令 终止进程

Linux中的kill命令用来终止指定的进程(terminate a process)的运行 通常,终止一个前台进程可以使用Ctrl+C键,但是,对于一个后台进程就须用kill命令来终止,我们就需要先使用ps/pidof/pstree/top等工具获取进程PID,然后使用kill命令来杀掉该进程.kill命令是通过向进程发送指定的信号来结束相应进程的.在默认情况下,采用编号为15的TERM信号.TERM信号将终止所有不能捕获该信号的进程.对于那些可以捕获该信号的进程就要用编号为9的kill信号,

[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

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

Linux查看用于终止进程命令

[email protected]:~$ ps PID TTY TIME CMD 2576 pts/0 00:00:00 bash 2742 pts/0 00:00:00 ps [email protected]:~$ kill -9 [2742] bash: kill: [2742]: 参数必须是进程或任务 ID -9 表示强迫进程立即停止 通常用 ps 查看进程 PID ,用 kill 命令终止进程

Linux查看命令终止进程

[email protected]:~$ ps PID TTY TIME CMD 2576 pts/0 00:00:00 bash 2742 pts/0 00:00:00 ps [email protected]:~$ kill -9 [2742] bash: kill: [2742]: 參数必须是进程或任务 ID -9 表示强迫进程马上停止 通经常使用 ps 查看流程 PID .使用 kill 命令终止进程 版权声明:本文博主原创文章,博客,未经同意不得转载.

我使用过的Linux命令之kill - 终止进程/发送信号

用途说明 kill命令用于终止指定的进程(terminate a process),是Unix/Linux下进程管理的常用命令.通常,我们在需要终止某个或某些进程时,先使用ps/pidof/pstree/top等工具获取进程PID,然后使用kill命令来杀掉该进程.kill命令的另外一个用途就是向指定的进程或进程组发送信号(The  command kill sends the specified signal to the specified process or process group)