-
Notifications
You must be signed in to change notification settings - Fork 43
/
kill-process.py
316 lines (279 loc) · 8.97 KB
/
kill-process.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
"""
582. Kill Process
Medium
You have n processes forming a rooted tree structure. You are given two integer arrays pid and ppid, where pid[i] is the ID of the ith process and ppid[i] is the ID of the ith process's parent process.
Each process has only one parent process but may have multiple children processes. Only one process has ppid[i] = 0, which means this process has no parent process (the root of the tree).
When a process is killed, all of its children processes will also be killed.
Given an integer kill representing the ID of a process you want to kill, return a list of the IDs of the processes that will be killed. You may return the answer in any order.
Example 1:
Input: pid = [1,3,10,5], ppid = [3,0,5,3], kill = 5
Output: [5,10]
Explanation: The processes colored in red are the processes that should be killed.
Example 2:
Input: pid = [1], ppid = [0], kill = 1
Output: [1]
Constraints:
n == pid.length
n == ppid.length
1 <= n <= 5 * 104
1 <= pid[i] <= 5 * 104
0 <= ppid[i] <= 5 * 104
Only one process has no parent.
All the values of pid are unique.
kill is guaranteed to be in pid.
"""
# V0
# IDEA : BFS + defaultdict
from collections import defaultdict
class Solution(object):
def killProcess(self, pid, ppid, kill):
d = defaultdict(set)
for a, b in zip(ppid, pid):
d[a].add(b)
q = [kill]
res = []
while q:
for i in range(len(q)):
tmp = q.pop(-1) # TODO check : should be pop(0) ???
res.append(tmp)
for _ in d[tmp]:
q.append(_)
return res
# V0'
# IDEA : DFS
class Solution:
def killProcess(self, pid, ppid, kill):
n = len(pid)
child = {}
for i in range(n):
if ppid[i] not in child:
child[ppid[i]] = []
child[ppid[i]].append(pid[i])
ans = []
self.dfs(kill, ans, child)
return ans
def dfs(self, now, ans, child):
ans.append(now)
if now in child:
for nxt in child[now]:
self.dfs(nxt, ans, child)
# V1
# IDEA : BFS
# http://bookshadow.com/weblog/2017/05/15/leetcode-kill-process/
class Solution(object):
def killProcess(self, pid, ppid, kill):
dic = collections.defaultdict(set)
for child, parent in zip(pid, ppid):
dic[parent].add(child)
queue = [kill]
victims = []
while queue:
first = queue.pop(0)
victims.append(first)
for child in dic[first]:
queue.append(child)
return victims
# V1'
# https://www.jiuzhang.com/solution/582-kill-process/#tag-highlight-lang-python
# IDEA : DFS
class Solution:
"""
@param pid: the process id
@param ppid: the parent process id
@param kill: a PID you want to kill
@return: a list of PIDs of processes that will be killed in the end
"""
def killProcess(self, pid, ppid, kill):
n = len(pid)
child = {}
for i in range(n):
if ppid[i] not in child:
child[ppid[i]] = []
child[ppid[i]].append(pid[i])
ans = []
self.dfs(kill, ans, child)
return ans
def dfs(self, now, ans, child):
ans.append(now)
if now in child:
for nxt in child[now]:
self.dfs(nxt, ans, child)
# V1''
# https://www.jiuzhang.com/solution/582-kill-process/#tag-highlight-lang-python
# IDEA : BFS
class Solution:
"""
@param pid: the process id
@param ppid: the parent process id
@param kill: a PID you want to kill
@return: a list of PIDs of processes that will be killed in the end
"""
def killProcess(self, pid, ppid, kill):
m = {}
for i, parent in enumerate(ppid):
if parent not in m:
m[parent] = []
m[parent].append(pid[i])
queue = [kill]
answer = []
while queue:
curr = queue.pop(0)
answer.append(curr)
if curr in m:
queue += m[curr]
return answer
# V1
# IDEA : DFS (TLE)
# https://leetcode.com/problems/kill-process/solution/
# JAVA
# public class Solution {
#
# public List < Integer > killProcess(List < Integer > pid, List < Integer > ppid, int kill) {
# List < Integer > l = new ArrayList < > ();
# if (kill == 0)
# return l;
# l.add(kill);
# for (int i = 0; i < ppid.size(); i++)
# if (ppid.get(i) == kill)
# l.addAll(killProcess(pid, ppid, pid.get(i)));
# return l;
# }
# }
# V1
# IDEA : Tree Simulation
# https://leetcode.com/problems/kill-process/solution/
# public class Solution {
# class Node {
# int val;
# List < Node > children = new ArrayList < > ();
# }
# public List < Integer > killProcess(List < Integer > pid, List < Integer > ppid, int kill) {
# HashMap < Integer, Node > map = new HashMap < > ();
# for (int id: pid) {
# Node node = new Node();
# node.val = id;
# map.put(id, node);
# }
# for (int i = 0; i < ppid.size(); i++) {
# if (ppid.get(i) > 0) {
# Node par = map.get(ppid.get(i));
# par.children.add(map.get(pid.get(i)));
# }
# }
# List < Integer > l = new ArrayList < > ();
# l.add(kill);
# getAllChildren(map.get(kill), l);
# return l;
# }
# public void getAllChildren(Node pn, List < Integer > l) {
# for (Node n: pn.children) {
# l.add(n.val);
# getAllChildren(n, l);
# }
# }
# }
# V1
# IDEA : HashMap + Depth First Search
# https://leetcode.com/problems/kill-process/solution/
# JAVA
# public class Solution {
# public List < Integer > killProcess(List < Integer > pid, List < Integer > ppid, int kill) {
# HashMap < Integer, List < Integer >> map = new HashMap < > ();
# for (int i = 0; i < ppid.size(); i++) {
# if (ppid.get(i) > 0) {
# List < Integer > l = map.getOrDefault(ppid.get(i), new ArrayList < Integer > ());
# l.add(pid.get(i));
# map.put(ppid.get(i), l);
# }
# }
# List < Integer > l = new ArrayList < > ();
# l.add(kill);
# getAllChildren(map, l, kill);
# return l;
# }
# public void getAllChildren(HashMap < Integer, List < Integer >> map, List < Integer > l, int kill) {
# if (map.containsKey(kill))
# for (int id: map.get(kill)) {
# l.add(id);
# getAllChildren(map, l, id);
# }
# }
# }
# V1
# IDEA : HashMap + Breadth First Search
# https://leetcode.com/problems/kill-process/solution/
# JAVA
# public class Solution {
#
# public List < Integer > killProcess(List < Integer > pid, List < Integer > ppid, int kill) {
# HashMap < Integer, List < Integer >> map = new HashMap < > ();
# for (int i = 0; i < ppid.size(); i++) {
# if (ppid.get(i) > 0) {
# List < Integer > l = map.getOrDefault(ppid.get(i), new ArrayList < Integer > ());
# l.add(pid.get(i));
# map.put(ppid.get(i), l);
# }
# }
# Queue < Integer > queue = new LinkedList < > ();
# List < Integer > l = new ArrayList < > ();
# queue.add(kill);
# while (!queue.isEmpty()) {
# int r = queue.remove();
# l.add(r);
# if (map.containsKey(r))
# for (int id: map.get(r))
# queue.add(id);
# }
# return l;
# }
# }
# V2
# Time: O(n)
# Space: O(n)
import collections
# DFS solution.
class Solution(object):
def killProcess(self, pid, ppid, kill):
"""
:type pid: List[int]
:type ppid: List[int]
:type kill: int
:rtype: List[int]
"""
def killAll(pid, children, killed):
killed.append(pid)
for child in children[pid]:
killAll(child, children, killed)
result = []
children = collections.defaultdict(set)
for i in range(len(pid)):
children[ppid[i]].add(pid[i])
killAll(kill, children, result)
return result
# Time: O(n)
# Space: O(n)
# BFS solution.
class Solution2(object):
def killProcess(self, pid, ppid, kill):
"""
:type pid: List[int]
:type ppid: List[int]
:type kill: int
:rtype: List[int]
"""
def killAll(pid, children, killed):
killed.append(pid)
for child in children[pid]:
killAll(child, children, killed)
result = []
children = collections.defaultdict(set)
for i in range(len(pid)):
children[ppid[i]].add(pid[i])
q = collections.deque()
q.append(kill)
while q:
p = q.popleft()
result.append(p)
for child in children[p]:
q.append(child)
return result