diff --git a/solution/0500-0599/0582.Kill Process/README.md b/solution/0500-0599/0582.Kill Process/README.md index f02fcfef1f247..e7ae1b121a0ef 100644 --- a/solution/0500-0599/0582.Kill Process/README.md +++ b/solution/0500-0599/0582.Kill Process/README.md @@ -49,6 +49,12 @@ +**方法一:DFS** + +我们先根据 $pid$ 和 $ppid$ 构建出图 $g$,其中 $g[i]$ 表示进程 $i$ 的所有子进程。然后从进程 $kill$ 开始,进行深度优先搜索,即可得到所有被杀掉的进程。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是进程的数量。 + ### **Python3** @@ -58,15 +64,14 @@ ```python class Solution: def killProcess(self, pid: List[int], ppid: List[int], kill: int) -> List[int]: - def dfs(u): - ans.append(u) - for v in g[u]: - dfs(v) + def dfs(i: int): + ans.append(i) + for j in g[i]: + dfs(j) g = defaultdict(list) - n = len(pid) - for c, p in zip(pid, ppid): - g[p].append(c) + for i, p in zip(pid, ppid): + g[p].append(i) ans = [] dfs(kill) return ans @@ -78,24 +83,22 @@ class Solution: ```java class Solution { - private Map> g; - private List ans; + private Map> g = new HashMap<>(); + private List ans = new ArrayList<>(); public List killProcess(List pid, List ppid, int kill) { - g = new HashMap<>(); - for (int i = 0, n = pid.size(); i < n; ++i) { - int c = pid.get(i), p = ppid.get(i); - g.computeIfAbsent(p, k -> new ArrayList<>()).add(c); + int n = pid.size(); + for (int i = 0; i < n; ++i) { + g.computeIfAbsent(ppid.get(i), k -> new ArrayList<>()).add(pid.get(i)); } - ans = new ArrayList<>(); dfs(kill); return ans; } - private void dfs(int u) { - ans.add(u); - for (int v : g.getOrDefault(u, new ArrayList<>())) { - dfs(v); + private void dfs(int i) { + ans.add(i); + for (int j : g.getOrDefault(i, Collections.emptyList())) { + dfs(j); } } } @@ -108,42 +111,93 @@ class Solution { public: vector killProcess(vector& pid, vector& ppid, int kill) { unordered_map> g; - vector ans; int n = pid.size(); for (int i = 0; i < n; ++i) { - int c = pid[i], p = ppid[i]; - g[p].push_back(c); + g[ppid[i]].push_back(pid[i]); } - dfs(kill, g, ans); + vector ans; + function dfs = [&](int i) { + ans.push_back(i); + for (int j : g[i]) { + dfs(j); + } + }; + dfs(kill); return ans; } - - void dfs(int u, unordered_map>& g, vector& ans) { - ans.push_back(u); - for (int v : g[u]) dfs(v, g, ans); - } }; ``` ### **Go** ```go -func killProcess(pid []int, ppid []int, kill int) []int { - g := make(map[int][]int) - for i, c := range pid { - p := ppid[i] - g[p] = append(g[p], c) +func killProcess(pid []int, ppid []int, kill int) (ans []int) { + g := map[int][]int{} + for i, p := range ppid { + g[p] = append(g[p], pid[i]) } - var ans []int - var dfs func(u int) - dfs = func(u int) { - ans = append(ans, u) - for _, v := range g[u] { - dfs(v) + var dfs func(int) + dfs = func(i int) { + ans = append(ans, i) + for _, j := range g[i] { + dfs(j) } } dfs(kill) - return ans + return +} +``` + +### **TypeScript** + +```ts +function killProcess(pid: number[], ppid: number[], kill: number): number[] { + const g: Map = new Map(); + for (let i = 0; i < pid.length; ++i) { + if (!g.has(ppid[i])) { + g.set(ppid[i], []); + } + g.get(ppid[i])?.push(pid[i]); + } + const ans: number[] = []; + const dfs = (i: number) => { + ans.push(i); + for (const j of g.get(i) ?? []) { + dfs(j); + } + }; + dfs(kill); + return ans; +} +``` + +### **Rust** + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn kill_process(pid: Vec, ppid: Vec, kill: i32) -> Vec { + let mut g: HashMap> = HashMap::new(); + let mut ans: Vec = Vec::new(); + + let n = pid.len(); + for i in 0..n { + g.entry(ppid[i]).or_insert(Vec::new()).push(pid[i]); + } + + Self::dfs(&mut ans, &g, kill); + ans + } + + fn dfs(ans: &mut Vec, g: &HashMap>, i: i32) { + ans.push(i); + if let Some(children) = g.get(&i) { + for &j in children { + Self::dfs(ans, g, j); + } + } + } } ``` diff --git a/solution/0500-0599/0582.Kill Process/README_EN.md b/solution/0500-0599/0582.Kill Process/README_EN.md index 5995c55931ee7..436ea9f72e25c 100644 --- a/solution/0500-0599/0582.Kill Process/README_EN.md +++ b/solution/0500-0599/0582.Kill Process/README_EN.md @@ -44,6 +44,12 @@ ## Solutions +**Solution 1: DFS** + +We first construct a graph $g$ based on $pid$ and $ppid$, where $g[i]$ represents all child processes of process $i$. Then, starting from the process $kill$, we perform depth-first search to obtain all killed processes. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of processes. + ### **Python3** @@ -51,15 +57,14 @@ ```python class Solution: def killProcess(self, pid: List[int], ppid: List[int], kill: int) -> List[int]: - def dfs(u): - ans.append(u) - for v in g[u]: - dfs(v) + def dfs(i: int): + ans.append(i) + for j in g[i]: + dfs(j) g = defaultdict(list) - n = len(pid) - for c, p in zip(pid, ppid): - g[p].append(c) + for i, p in zip(pid, ppid): + g[p].append(i) ans = [] dfs(kill) return ans @@ -69,24 +74,22 @@ class Solution: ```java class Solution { - private Map> g; - private List ans; + private Map> g = new HashMap<>(); + private List ans = new ArrayList<>(); public List killProcess(List pid, List ppid, int kill) { - g = new HashMap<>(); - for (int i = 0, n = pid.size(); i < n; ++i) { - int c = pid.get(i), p = ppid.get(i); - g.computeIfAbsent(p, k -> new ArrayList<>()).add(c); + int n = pid.size(); + for (int i = 0; i < n; ++i) { + g.computeIfAbsent(ppid.get(i), k -> new ArrayList<>()).add(pid.get(i)); } - ans = new ArrayList<>(); dfs(kill); return ans; } - private void dfs(int u) { - ans.add(u); - for (int v : g.getOrDefault(u, new ArrayList<>())) { - dfs(v); + private void dfs(int i) { + ans.add(i); + for (int j : g.getOrDefault(i, Collections.emptyList())) { + dfs(j); } } } @@ -99,42 +102,93 @@ class Solution { public: vector killProcess(vector& pid, vector& ppid, int kill) { unordered_map> g; - vector ans; int n = pid.size(); for (int i = 0; i < n; ++i) { - int c = pid[i], p = ppid[i]; - g[p].push_back(c); + g[ppid[i]].push_back(pid[i]); } - dfs(kill, g, ans); + vector ans; + function dfs = [&](int i) { + ans.push_back(i); + for (int j : g[i]) { + dfs(j); + } + }; + dfs(kill); return ans; } - - void dfs(int u, unordered_map>& g, vector& ans) { - ans.push_back(u); - for (int v : g[u]) dfs(v, g, ans); - } }; ``` ### **Go** ```go -func killProcess(pid []int, ppid []int, kill int) []int { - g := make(map[int][]int) - for i, c := range pid { - p := ppid[i] - g[p] = append(g[p], c) +func killProcess(pid []int, ppid []int, kill int) (ans []int) { + g := map[int][]int{} + for i, p := range ppid { + g[p] = append(g[p], pid[i]) } - var ans []int - var dfs func(u int) - dfs = func(u int) { - ans = append(ans, u) - for _, v := range g[u] { - dfs(v) + var dfs func(int) + dfs = func(i int) { + ans = append(ans, i) + for _, j := range g[i] { + dfs(j) } } dfs(kill) - return ans + return +} +``` + +### **TypeScript** + +```ts +function killProcess(pid: number[], ppid: number[], kill: number): number[] { + const g: Map = new Map(); + for (let i = 0; i < pid.length; ++i) { + if (!g.has(ppid[i])) { + g.set(ppid[i], []); + } + g.get(ppid[i])?.push(pid[i]); + } + const ans: number[] = []; + const dfs = (i: number) => { + ans.push(i); + for (const j of g.get(i) ?? []) { + dfs(j); + } + }; + dfs(kill); + return ans; +} +``` + +### **Rust** + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn kill_process(pid: Vec, ppid: Vec, kill: i32) -> Vec { + let mut g: HashMap> = HashMap::new(); + let mut ans: Vec = Vec::new(); + + let n = pid.len(); + for i in 0..n { + g.entry(ppid[i]).or_insert(Vec::new()).push(pid[i]); + } + + Self::dfs(&mut ans, &g, kill); + ans + } + + fn dfs(ans: &mut Vec, g: &HashMap>, i: i32) { + ans.push(i); + if let Some(children) = g.get(&i) { + for &j in children { + Self::dfs(ans, g, j); + } + } + } } ``` diff --git a/solution/0500-0599/0582.Kill Process/Solution.cpp b/solution/0500-0599/0582.Kill Process/Solution.cpp index 9e39e908c015c..637c453ece01f 100644 --- a/solution/0500-0599/0582.Kill Process/Solution.cpp +++ b/solution/0500-0599/0582.Kill Process/Solution.cpp @@ -1,19 +1,19 @@ -class Solution { -public: - vector killProcess(vector& pid, vector& ppid, int kill) { - unordered_map> g; - vector ans; - int n = pid.size(); - for (int i = 0; i < n; ++i) { - int c = pid[i], p = ppid[i]; - g[p].push_back(c); - } - dfs(kill, g, ans); - return ans; - } - - void dfs(int u, unordered_map>& g, vector& ans) { - ans.push_back(u); - for (int v : g[u]) dfs(v, g, ans); - } +class Solution { +public: + vector killProcess(vector& pid, vector& ppid, int kill) { + unordered_map> g; + int n = pid.size(); + for (int i = 0; i < n; ++i) { + g[ppid[i]].push_back(pid[i]); + } + vector ans; + function dfs = [&](int i) { + ans.push_back(i); + for (int j : g[i]) { + dfs(j); + } + }; + dfs(kill); + return ans; + } }; \ No newline at end of file diff --git a/solution/0500-0599/0582.Kill Process/Solution.go b/solution/0500-0599/0582.Kill Process/Solution.go index 845a05d2be3fc..6aedb3e279d11 100644 --- a/solution/0500-0599/0582.Kill Process/Solution.go +++ b/solution/0500-0599/0582.Kill Process/Solution.go @@ -1,17 +1,15 @@ -func killProcess(pid []int, ppid []int, kill int) []int { - g := make(map[int][]int) - for i, c := range pid { - p := ppid[i] - g[p] = append(g[p], c) +func killProcess(pid []int, ppid []int, kill int) (ans []int) { + g := map[int][]int{} + for i, p := range ppid { + g[p] = append(g[p], pid[i]) } - var ans []int - var dfs func(u int) - dfs = func(u int) { - ans = append(ans, u) - for _, v := range g[u] { - dfs(v) + var dfs func(int) + dfs = func(i int) { + ans = append(ans, i) + for _, j := range g[i] { + dfs(j) } } dfs(kill) - return ans + return } \ No newline at end of file diff --git a/solution/0500-0599/0582.Kill Process/Solution.java b/solution/0500-0599/0582.Kill Process/Solution.java index 0e355f7211880..be3c68ca350d4 100644 --- a/solution/0500-0599/0582.Kill Process/Solution.java +++ b/solution/0500-0599/0582.Kill Process/Solution.java @@ -1,22 +1,20 @@ -class Solution { - private Map> g; - private List ans; - - public List killProcess(List pid, List ppid, int kill) { - g = new HashMap<>(); - for (int i = 0, n = pid.size(); i < n; ++i) { - int c = pid.get(i), p = ppid.get(i); - g.computeIfAbsent(p, k -> new ArrayList<>()).add(c); - } - ans = new ArrayList<>(); - dfs(kill); - return ans; - } - - private void dfs(int u) { - ans.add(u); - for (int v : g.getOrDefault(u, new ArrayList<>())) { - dfs(v); - } - } +class Solution { + private Map> g = new HashMap<>(); + private List ans = new ArrayList<>(); + + public List killProcess(List pid, List ppid, int kill) { + int n = pid.size(); + for (int i = 0; i < n; ++i) { + g.computeIfAbsent(ppid.get(i), k -> new ArrayList<>()).add(pid.get(i)); + } + dfs(kill); + return ans; + } + + private void dfs(int i) { + ans.add(i); + for (int j : g.getOrDefault(i, Collections.emptyList())) { + dfs(j); + } + } } \ No newline at end of file diff --git a/solution/0500-0599/0582.Kill Process/Solution.py b/solution/0500-0599/0582.Kill Process/Solution.py index 8cf9cd633cc80..e0e63f3743c53 100644 --- a/solution/0500-0599/0582.Kill Process/Solution.py +++ b/solution/0500-0599/0582.Kill Process/Solution.py @@ -1,14 +1,13 @@ -class Solution: - def killProcess(self, pid: List[int], ppid: List[int], kill: int) -> List[int]: - def dfs(u): - ans.append(u) - for v in g[u]: - dfs(v) - - g = defaultdict(list) - n = len(pid) - for c, p in zip(pid, ppid): - g[p].append(c) - ans = [] - dfs(kill) - return ans +class Solution: + def killProcess(self, pid: List[int], ppid: List[int], kill: int) -> List[int]: + def dfs(i: int): + ans.append(i) + for j in g[i]: + dfs(j) + + g = defaultdict(list) + for i, p in zip(pid, ppid): + g[p].append(i) + ans = [] + dfs(kill) + return ans diff --git a/solution/0500-0599/0582.Kill Process/Solution.rs b/solution/0500-0599/0582.Kill Process/Solution.rs new file mode 100644 index 0000000000000..808e1087ddaca --- /dev/null +++ b/solution/0500-0599/0582.Kill Process/Solution.rs @@ -0,0 +1,25 @@ +use std::collections::HashMap; + +impl Solution { + pub fn kill_process(pid: Vec, ppid: Vec, kill: i32) -> Vec { + let mut g: HashMap> = HashMap::new(); + let mut ans: Vec = Vec::new(); + + let n = pid.len(); + for i in 0..n { + g.entry(ppid[i]).or_insert(Vec::new()).push(pid[i]); + } + + Self::dfs(&mut ans, &g, kill); + ans + } + + fn dfs(ans: &mut Vec, g: &HashMap>, i: i32) { + ans.push(i); + if let Some(children) = g.get(&i) { + for &j in children { + Self::dfs(ans, g, j); + } + } + } +} \ No newline at end of file diff --git a/solution/0500-0599/0582.Kill Process/Solution.ts b/solution/0500-0599/0582.Kill Process/Solution.ts new file mode 100644 index 0000000000000..1e8e0e026acd4 --- /dev/null +++ b/solution/0500-0599/0582.Kill Process/Solution.ts @@ -0,0 +1,18 @@ +function killProcess(pid: number[], ppid: number[], kill: number): number[] { + const g: Map = new Map(); + for (let i = 0; i < pid.length; ++i) { + if (!g.has(ppid[i])) { + g.set(ppid[i], []); + } + g.get(ppid[i])?.push(pid[i]); + } + const ans: number[] = []; + const dfs = (i: number) => { + ans.push(i); + for (const j of g.get(i) ?? []) { + dfs(j); + } + }; + dfs(kill); + return ans; +}