Skip to content

Commit a07c800

Browse files
committed
2021-03-13
1 parent 10422e1 commit a07c800

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution(object):
2+
def minOperations(self, logs):
3+
"""
4+
:type logs: List[str]
5+
:rtype: int
6+
"""
7+
steps = 0
8+
for log in logs:
9+
if log == "../" and steps:
10+
steps -= 1
11+
elif log not in ["../", "./"]:
12+
steps += 1
13+
return steps
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class ThroneInheritance(object):
2+
def __init__(self, kingName):
3+
"""
4+
:type kingName: str
5+
"""
6+
from collections import defaultdict
7+
self.par2child = defaultdict(list)
8+
self.kingName = kingName
9+
self.deaths = set()
10+
11+
def birth(self, parentName, childName):
12+
"""
13+
:type parentName: str
14+
:type childName: str
15+
:rtype: None
16+
"""
17+
self.par2child[parentName].append(childName)
18+
19+
20+
def death(self, name):
21+
"""
22+
:type name: str
23+
:rtype: None
24+
"""
25+
self.deaths.add(name)
26+
27+
def getInheritanceOrder(self):
28+
"""
29+
:rtype: List[str]
30+
"""
31+
return self.preorder(self.kingName)
32+
33+
def preorder(self, cur):
34+
res = []
35+
if cur not in self.deaths:
36+
res.append(cur)
37+
38+
for child in self.par2child[cur]:
39+
res += self.preorder(child)
40+
return res
41+
42+
43+
44+
45+
46+
# Your ThroneInheritance object will be instantiated and called as such:
47+
# obj = ThroneInheritance(kingName)
48+
# obj.birth(parentName,childName)
49+
# obj.death(name)
50+
# param_3 = obj.getInheritanceOrder()

0 commit comments

Comments
 (0)