Skip to content

feat: add solutions to lc problem: No.0582 #1831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 93 additions & 39 deletions solution/0500-0599/0582.Kill Process/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@

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

**方法一:DFS**

我们先根据 $pid$ 和 $ppid$ 构建出图 $g$,其中 $g[i]$ 表示进程 $i$ 的所有子进程。然后从进程 $kill$ 开始,进行深度优先搜索,即可得到所有被杀掉的进程。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是进程的数量。

<!-- tabs:start -->

### **Python3**
Expand All @@ -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
Expand All @@ -78,24 +83,22 @@ class Solution:

```java
class Solution {
private Map<Integer, List<Integer>> g;
private List<Integer> ans;
private Map<Integer, List<Integer>> g = new HashMap<>();
private List<Integer> ans = new ArrayList<>();

public List<Integer> killProcess(List<Integer> pid, List<Integer> 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);
}
}
}
Expand All @@ -108,42 +111,93 @@ class Solution {
public:
vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
unordered_map<int, vector<int>> g;
vector<int> 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<int> ans;
function<void(int)> dfs = [&](int i) {
ans.push_back(i);
for (int j : g[i]) {
dfs(j);
}
};
dfs(kill);
return ans;
}

void dfs(int u, unordered_map<int, vector<int>>& g, vector<int>& 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<number, number[]> = 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<i32>, ppid: Vec<i32>, kill: i32) -> Vec<i32> {
let mut g: HashMap<i32, Vec<i32>> = HashMap::new();
let mut ans: Vec<i32> = 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<i32>, g: &HashMap<i32, Vec<i32>>, i: i32) {
ans.push(i);
if let Some(children) = g.get(&i) {
for &j in children {
Self::dfs(ans, g, j);
}
}
}
}
```

Expand Down
132 changes: 93 additions & 39 deletions solution/0500-0599/0582.Kill Process/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,27 @@

## 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.

<!-- tabs:start -->

### **Python3**

```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
Expand All @@ -69,24 +74,22 @@ class Solution:

```java
class Solution {
private Map<Integer, List<Integer>> g;
private List<Integer> ans;
private Map<Integer, List<Integer>> g = new HashMap<>();
private List<Integer> ans = new ArrayList<>();

public List<Integer> killProcess(List<Integer> pid, List<Integer> 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);
}
}
}
Expand All @@ -99,42 +102,93 @@ class Solution {
public:
vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
unordered_map<int, vector<int>> g;
vector<int> 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<int> ans;
function<void(int)> dfs = [&](int i) {
ans.push_back(i);
for (int j : g[i]) {
dfs(j);
}
};
dfs(kill);
return ans;
}

void dfs(int u, unordered_map<int, vector<int>>& g, vector<int>& 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<number, number[]> = 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<i32>, ppid: Vec<i32>, kill: i32) -> Vec<i32> {
let mut g: HashMap<i32, Vec<i32>> = HashMap::new();
let mut ans: Vec<i32> = 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<i32>, g: &HashMap<i32, Vec<i32>>, i: i32) {
ans.push(i);
if let Some(children) = g.get(&i) {
for &j in children {
Self::dfs(ans, g, j);
}
}
}
}
```

Expand Down
Loading