File tree 2 files changed +63
-0
lines changed
2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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()
You can’t perform that action at this time.
0 commit comments