资讯专栏INFORMATION COLUMN

[LeetCode] Kill Process

jone5679 / 486人阅读

Problem

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

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.

Solution
class Solution {
    public List killProcess(List pid, List ppid, int kill) {
        List res = new ArrayList<>();
        if (pid == null || pid.size() == 0) return res;
        Map> map = new HashMap<>();
        for (Integer id: pid) {
            map.put(id, new HashSet());
        }
        for (int i = 0; i < ppid.size(); i++) {
            int id = ppid.get(i);
            if (map.containsKey(id)) {
                map.get(id).add(pid.get(i));
            }
        }
        traverse(kill, map, res);
        return res;
    }
    public void traverse(int kill, Map> map, List res) {
        res.add(kill);
        Set children = map.get(kill);
        for (Integer child: children) {
            traverse(child, map, res);
        }
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/76791.html

相关文章

  • Python实现配置热加载的方法

      小编写这篇文章的目的,主要是给大家讲解一下,关于实现配置热加载的方法,具体是怎么操作呢?下面就给大家详细的解答下。  背景  由于最近有相关的工作需求,需要进行增添相关的新功能,实现配置热加载的功能。所谓的配置热加载,也就是说当服务收到配置更新消息之后,我们不用重启服务就可以使用最新的配置去执行任务。  如何实现  下面我分别采用多进程、多线程、协程的方式去实现配置热加载。  使用多进程实现配...

    89542767 评论0 收藏0
  • 优雅地关闭kubernetes中的nginx

    摘要:被设计为这样一种方式,父进程必须明确地等待子进程终止,以便收集它的退出状态。会完成的删除,将优雅退出的时间设置为表示立即删除。 SIGINT SIGTERM SIGKILL区别 三者都是结束/终止进程运行。 1.SIGINT SIGTERM区别 前者与字符ctrl+c关联,后者没有任何控制字符关联。前者只能结束前台进程,后者则不是。 2.SIGTERM SIGKILL的区别 前者可以被...

    Noodles 评论0 收藏0
  • 优雅地关闭kubernetes中的nginx

    摘要:被设计为这样一种方式,父进程必须明确地等待子进程终止,以便收集它的退出状态。会完成的删除,将优雅退出的时间设置为表示立即删除。 SIGINT SIGTERM SIGKILL区别 三者都是结束/终止进程运行。 1.SIGINT SIGTERM区别 前者与字符ctrl+c关联,后者没有任何控制字符关联。前者只能结束前台进程,后者则不是。 2.SIGTERM SIGKILL的区别 前者可以被...

    余学文 评论0 收藏0
  • 模拟nginx热部署

    摘要:热部署,就是在应用正在运行的时候升级软件,却不需要重新启动应用。热部署的流程是备份旧的可执行文件新的可执行文件直接替换旧的此时旧的进程还在运行向进程发送热部署信号,新的进程启动,旧的不再就收请求。关闭旧的进程,完成热部署。 热部署,就是在应用正在运行的时候升级软件,却不需要重新启动应用。 首先在本地模拟一个线上需要升级 Nginx 的环境,假设旧版本为 nginx-1.0.15,需要升...

    caige 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<