-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
582 Kill Process.swift
46 lines (38 loc) 路 1.23 KB
/
582 Kill Process.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// 582 Kill Process.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 15/02/2021.
// Copyright 漏 2021 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/kill-process/
class Solution {
/// Finds a list of PIDs of processes that will be killed.
///
/// - Parameters:
/// - pid: A list of the process IDs.
/// - ppid: A list of process parent ID.
/// - kill: An ID of the process to kill.
/// - Returns: A list of PIDs.
///
/// - Complexity:
/// - time: O(n), where n is the number of processes.
/// - space: O(n), where n is the number of processes.
func killProcess(_ pid: [Int], _ ppid: [Int], _ kill: Int) -> [Int] {
var dict = [Int: [Int]]()
for (i, id) in pid.enumerated() {
dict[ppid[i], default: [Int]()].append(id)
}
var ans = [Int]()
var stack = [Int]()
stack.append(kill)
while !stack.isEmpty {
let cur = stack.removeLast()
ans.append(cur)
guard let children = dict[cur] else { continue }
stack.append(contentsOf: children)
}
return ans
}
}