-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkill_process.py
More file actions
32 lines (25 loc) · 865 Bytes
/
kill_process.py
File metadata and controls
32 lines (25 loc) · 865 Bytes
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
# Problem: Kill Process
# (https://leetcode.com/problems/kill-process/#/description)
# This can be done without building the tree by recording the indices in the
# hash table.
from collections import defaultdict
class Solution(object):
killList = []
def collectNodes(self, pid, adjList):
self.killList.append(pid)
for child in adjList[pid]:
self.collectNodes(child, adjList)
def killProcess(self, pid, ppid, kill):
"""
:type pid: List[int]
:type ppid: List[int]
:type kill: int
:rtype: List[int]
"""
self.killList = []
adjList = defaultdict(list)
adjList[0] = []
for i in range(len(pid)):
adjList[ppid[i]].append(pid[i])
self.collectNodes(kill, adjList)
return self.killList