-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path1600.throne-inheritance.py
61 lines (47 loc) · 1.66 KB
/
1600.throne-inheritance.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
#
# @lc app=leetcode id=1600 lang=python3
#
# [1600] Throne Inheritance
#
# @lc code=start
# TAGS: Tree, Design
class ThroneInheritance:
# 752 ms, 90.45%
def __init__(self, kingName: str):
self.graph = collections.defaultdict(list)
self.deaths = set()
self.first_king = kingName
def birth(self, parentName: str, childName: str) -> None:
self.graph[parentName].append(childName)
def death(self, name: str) -> None:
self.deaths.add(name)
def getInheritanceOrder(self) -> List[str]:
inheritance_order = []
stack = [self.first_king]
while stack:
person = stack.pop()
if person not in self.deaths:
inheritance_order.append(person)
stack.extend(self.graph[person][::-1])
return inheritance_order
# Similar to above but recursion.
def getInheritanceOrder(self) -> List[str]:
def dfs(person, inheritance):
if person not in self.deaths:
inheritance.append(person)
for child in self.graph[person]:
dfs(child, inheritance)
inheritance_order = []
dfs(self.first_king, inheritance_order)
return inheritance_order
# Your ThroneInheritance object will be instantiated and called as such:
# obj = ThroneInheritance(kingName)
# obj.birth(parentName,childName)
# obj.death(name)
# param_3 = obj.getInheritanceOrder()
# Your ThroneInheritance object will be instantiated and called as such:
# obj = ThroneInheritance(kingName)
# obj.birth(parentName,childName)
# obj.death(name)
# param_3 = obj.getInheritanceOrder()
# @lc code=end