Skip to content

Commit c3f49b4

Browse files
committed
Runtime: 1338 ms (Top 35.29%) | Memory: 68.8 MB (Top 82.35%)
1 parent 842117b commit c3f49b4

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Runtime: 1338 ms (Top 35.29%) | Memory: 68.8 MB (Top 82.35%)
2+
13
class ThroneInheritance:
24

35
def __init__(self, kingName: str):
@@ -7,14 +9,14 @@ def __init__(self, kingName: str):
79
# notDead will hold all the people who are alive and their level number
810
self.alive = {}
911
self.alive[kingName] = 0
10-
12+
1113
# hold edges existing in our graph
1214
self.edges = {self.root:[]}
13-
15+
1416
def birth(self, parentName: str, childName: str) -> None:
1517
# birth --> new child so update alive
1618
self.alive[childName] = self.alive[parentName]+1
17-
19+
1820
# add parent to child edges in the edges dictionary
1921
if parentName in self.edges:
2022
self.edges[parentName].append(childName)
@@ -24,25 +26,24 @@ def birth(self, parentName: str, childName: str) -> None:
2426
if childName not in self.edges:
2527
self.edges[childName] = []
2628
self.edges[parentName] = [childName]
27-
28-
29+
2930
def death(self, name: str) -> None:
3031
# removing the dead people from alive map
3132
del self.alive[name]
32-
33+
3334
def getInheritanceOrder(self) -> List[str]:
34-
35+
3536
hierarchy = []
3637
def dfs(cur,parent=-1):
3738
nonlocal hierarchy
38-
39+
3940
# current person available in alive then only add in hierarchy
4041
if cur in self.alive:
4142
hierarchy.append(cur)
42-
43+
4344
# traverse all the children of current node
4445
for i in self.edges[cur]:
4546
if i!=parent:
4647
dfs(i,cur)
4748
dfs(self.root)
48-
return hierarchy
49+
return hierarchy

0 commit comments

Comments
 (0)