Skip to content

Commit bb69b7d

Browse files
authored
feat: add solutions to lc problem: No.0582 (#1831)
No.0582.Kill Process
1 parent c0c0274 commit bb69b7d

File tree

8 files changed

+289
-143
lines changed

8 files changed

+289
-143
lines changed

solution/0500-0599/0582.Kill Process/README.md

+93-39
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
**方法一:DFS**
53+
54+
我们先根据 $pid$ 和 $ppid$ 构建出图 $g$,其中 $g[i]$ 表示进程 $i$ 的所有子进程。然后从进程 $kill$ 开始,进行深度优先搜索,即可得到所有被杀掉的进程。
55+
56+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是进程的数量。
57+
5258
<!-- tabs:start -->
5359

5460
### **Python3**
@@ -58,15 +64,14 @@
5864
```python
5965
class Solution:
6066
def killProcess(self, pid: List[int], ppid: List[int], kill: int) -> List[int]:
61-
def dfs(u):
62-
ans.append(u)
63-
for v in g[u]:
64-
dfs(v)
67+
def dfs(i: int):
68+
ans.append(i)
69+
for j in g[i]:
70+
dfs(j)
6571

6672
g = defaultdict(list)
67-
n = len(pid)
68-
for c, p in zip(pid, ppid):
69-
g[p].append(c)
73+
for i, p in zip(pid, ppid):
74+
g[p].append(i)
7075
ans = []
7176
dfs(kill)
7277
return ans
@@ -78,24 +83,22 @@ class Solution:
7883

7984
```java
8085
class Solution {
81-
private Map<Integer, List<Integer>> g;
82-
private List<Integer> ans;
86+
private Map<Integer, List<Integer>> g = new HashMap<>();
87+
private List<Integer> ans = new ArrayList<>();
8388

8489
public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
85-
g = new HashMap<>();
86-
for (int i = 0, n = pid.size(); i < n; ++i) {
87-
int c = pid.get(i), p = ppid.get(i);
88-
g.computeIfAbsent(p, k -> new ArrayList<>()).add(c);
90+
int n = pid.size();
91+
for (int i = 0; i < n; ++i) {
92+
g.computeIfAbsent(ppid.get(i), k -> new ArrayList<>()).add(pid.get(i));
8993
}
90-
ans = new ArrayList<>();
9194
dfs(kill);
9295
return ans;
9396
}
9497

95-
private void dfs(int u) {
96-
ans.add(u);
97-
for (int v : g.getOrDefault(u, new ArrayList<>())) {
98-
dfs(v);
98+
private void dfs(int i) {
99+
ans.add(i);
100+
for (int j : g.getOrDefault(i, Collections.emptyList())) {
101+
dfs(j);
99102
}
100103
}
101104
}
@@ -108,42 +111,93 @@ class Solution {
108111
public:
109112
vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
110113
unordered_map<int, vector<int>> g;
111-
vector<int> ans;
112114
int n = pid.size();
113115
for (int i = 0; i < n; ++i) {
114-
int c = pid[i], p = ppid[i];
115-
g[p].push_back(c);
116+
g[ppid[i]].push_back(pid[i]);
116117
}
117-
dfs(kill, g, ans);
118+
vector<int> ans;
119+
function<void(int)> dfs = [&](int i) {
120+
ans.push_back(i);
121+
for (int j : g[i]) {
122+
dfs(j);
123+
}
124+
};
125+
dfs(kill);
118126
return ans;
119127
}
120-
121-
void dfs(int u, unordered_map<int, vector<int>>& g, vector<int>& ans) {
122-
ans.push_back(u);
123-
for (int v : g[u]) dfs(v, g, ans);
124-
}
125128
};
126129
```
127130
128131
### **Go**
129132
130133
```go
131-
func killProcess(pid []int, ppid []int, kill int) []int {
132-
g := make(map[int][]int)
133-
for i, c := range pid {
134-
p := ppid[i]
135-
g[p] = append(g[p], c)
134+
func killProcess(pid []int, ppid []int, kill int) (ans []int) {
135+
g := map[int][]int{}
136+
for i, p := range ppid {
137+
g[p] = append(g[p], pid[i])
136138
}
137-
var ans []int
138-
var dfs func(u int)
139-
dfs = func(u int) {
140-
ans = append(ans, u)
141-
for _, v := range g[u] {
142-
dfs(v)
139+
var dfs func(int)
140+
dfs = func(i int) {
141+
ans = append(ans, i)
142+
for _, j := range g[i] {
143+
dfs(j)
143144
}
144145
}
145146
dfs(kill)
146-
return ans
147+
return
148+
}
149+
```
150+
151+
### **TypeScript**
152+
153+
```ts
154+
function killProcess(pid: number[], ppid: number[], kill: number): number[] {
155+
const g: Map<number, number[]> = new Map();
156+
for (let i = 0; i < pid.length; ++i) {
157+
if (!g.has(ppid[i])) {
158+
g.set(ppid[i], []);
159+
}
160+
g.get(ppid[i])?.push(pid[i]);
161+
}
162+
const ans: number[] = [];
163+
const dfs = (i: number) => {
164+
ans.push(i);
165+
for (const j of g.get(i) ?? []) {
166+
dfs(j);
167+
}
168+
};
169+
dfs(kill);
170+
return ans;
171+
}
172+
```
173+
174+
### **Rust**
175+
176+
```rust
177+
use std::collections::HashMap;
178+
179+
impl Solution {
180+
pub fn kill_process(pid: Vec<i32>, ppid: Vec<i32>, kill: i32) -> Vec<i32> {
181+
let mut g: HashMap<i32, Vec<i32>> = HashMap::new();
182+
let mut ans: Vec<i32> = Vec::new();
183+
184+
let n = pid.len();
185+
for i in 0..n {
186+
g.entry(ppid[i]).or_insert(Vec::new()).push(pid[i]);
187+
}
188+
189+
Self::dfs(&mut ans, &g, kill);
190+
ans
191+
}
192+
193+
fn dfs(ans: &mut Vec<i32>, g: &HashMap<i32, Vec<i32>>, i: i32) {
194+
ans.push(i);
195+
if let Some(children) = g.get(&i) {
196+
for &j in children {
197+
Self::dfs(ans, g, j);
198+
}
199+
}
200+
}
147201
}
148202
```
149203

solution/0500-0599/0582.Kill Process/README_EN.md

+93-39
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,27 @@
4444

4545
## Solutions
4646

47+
**Solution 1: DFS**
48+
49+
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.
50+
51+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of processes.
52+
4753
<!-- tabs:start -->
4854

4955
### **Python3**
5056

5157
```python
5258
class Solution:
5359
def killProcess(self, pid: List[int], ppid: List[int], kill: int) -> List[int]:
54-
def dfs(u):
55-
ans.append(u)
56-
for v in g[u]:
57-
dfs(v)
60+
def dfs(i: int):
61+
ans.append(i)
62+
for j in g[i]:
63+
dfs(j)
5864

5965
g = defaultdict(list)
60-
n = len(pid)
61-
for c, p in zip(pid, ppid):
62-
g[p].append(c)
66+
for i, p in zip(pid, ppid):
67+
g[p].append(i)
6368
ans = []
6469
dfs(kill)
6570
return ans
@@ -69,24 +74,22 @@ class Solution:
6974

7075
```java
7176
class Solution {
72-
private Map<Integer, List<Integer>> g;
73-
private List<Integer> ans;
77+
private Map<Integer, List<Integer>> g = new HashMap<>();
78+
private List<Integer> ans = new ArrayList<>();
7479

7580
public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
76-
g = new HashMap<>();
77-
for (int i = 0, n = pid.size(); i < n; ++i) {
78-
int c = pid.get(i), p = ppid.get(i);
79-
g.computeIfAbsent(p, k -> new ArrayList<>()).add(c);
81+
int n = pid.size();
82+
for (int i = 0; i < n; ++i) {
83+
g.computeIfAbsent(ppid.get(i), k -> new ArrayList<>()).add(pid.get(i));
8084
}
81-
ans = new ArrayList<>();
8285
dfs(kill);
8386
return ans;
8487
}
8588

86-
private void dfs(int u) {
87-
ans.add(u);
88-
for (int v : g.getOrDefault(u, new ArrayList<>())) {
89-
dfs(v);
89+
private void dfs(int i) {
90+
ans.add(i);
91+
for (int j : g.getOrDefault(i, Collections.emptyList())) {
92+
dfs(j);
9093
}
9194
}
9295
}
@@ -99,42 +102,93 @@ class Solution {
99102
public:
100103
vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
101104
unordered_map<int, vector<int>> g;
102-
vector<int> ans;
103105
int n = pid.size();
104106
for (int i = 0; i < n; ++i) {
105-
int c = pid[i], p = ppid[i];
106-
g[p].push_back(c);
107+
g[ppid[i]].push_back(pid[i]);
107108
}
108-
dfs(kill, g, ans);
109+
vector<int> ans;
110+
function<void(int)> dfs = [&](int i) {
111+
ans.push_back(i);
112+
for (int j : g[i]) {
113+
dfs(j);
114+
}
115+
};
116+
dfs(kill);
109117
return ans;
110118
}
111-
112-
void dfs(int u, unordered_map<int, vector<int>>& g, vector<int>& ans) {
113-
ans.push_back(u);
114-
for (int v : g[u]) dfs(v, g, ans);
115-
}
116119
};
117120
```
118121
119122
### **Go**
120123
121124
```go
122-
func killProcess(pid []int, ppid []int, kill int) []int {
123-
g := make(map[int][]int)
124-
for i, c := range pid {
125-
p := ppid[i]
126-
g[p] = append(g[p], c)
125+
func killProcess(pid []int, ppid []int, kill int) (ans []int) {
126+
g := map[int][]int{}
127+
for i, p := range ppid {
128+
g[p] = append(g[p], pid[i])
127129
}
128-
var ans []int
129-
var dfs func(u int)
130-
dfs = func(u int) {
131-
ans = append(ans, u)
132-
for _, v := range g[u] {
133-
dfs(v)
130+
var dfs func(int)
131+
dfs = func(i int) {
132+
ans = append(ans, i)
133+
for _, j := range g[i] {
134+
dfs(j)
134135
}
135136
}
136137
dfs(kill)
137-
return ans
138+
return
139+
}
140+
```
141+
142+
### **TypeScript**
143+
144+
```ts
145+
function killProcess(pid: number[], ppid: number[], kill: number): number[] {
146+
const g: Map<number, number[]> = new Map();
147+
for (let i = 0; i < pid.length; ++i) {
148+
if (!g.has(ppid[i])) {
149+
g.set(ppid[i], []);
150+
}
151+
g.get(ppid[i])?.push(pid[i]);
152+
}
153+
const ans: number[] = [];
154+
const dfs = (i: number) => {
155+
ans.push(i);
156+
for (const j of g.get(i) ?? []) {
157+
dfs(j);
158+
}
159+
};
160+
dfs(kill);
161+
return ans;
162+
}
163+
```
164+
165+
### **Rust**
166+
167+
```rust
168+
use std::collections::HashMap;
169+
170+
impl Solution {
171+
pub fn kill_process(pid: Vec<i32>, ppid: Vec<i32>, kill: i32) -> Vec<i32> {
172+
let mut g: HashMap<i32, Vec<i32>> = HashMap::new();
173+
let mut ans: Vec<i32> = Vec::new();
174+
175+
let n = pid.len();
176+
for i in 0..n {
177+
g.entry(ppid[i]).or_insert(Vec::new()).push(pid[i]);
178+
}
179+
180+
Self::dfs(&mut ans, &g, kill);
181+
ans
182+
}
183+
184+
fn dfs(ans: &mut Vec<i32>, g: &HashMap<i32, Vec<i32>>, i: i32) {
185+
ans.push(i);
186+
if let Some(children) = g.get(&i) {
187+
for &j in children {
188+
Self::dfs(ans, g, j);
189+
}
190+
}
191+
}
138192
}
139193
```
140194

0 commit comments

Comments
 (0)