exclusive time of functions

🏠
 1 from dataclasses import dataclass
 2 
 3 @dataclass
 4 class StackEntry:
 5     end: int
 6     duration: int
 7 
 8 @dataclass
 9 class Op:
10     id: int
11     op: str
12     time: int
13 
14 class Solution:
15     def exclusiveTime(self, n, logs):
16         res, stack = [0] * n, []
17         for task in map(lambda x: Op(int(x[0]), x[1], int(x[2])), (x.split(":") for x in logs)):
18             if task.op == "start":
19                 stack.append(StackEntry(end=task.time, duration=0))
20             elif task.op == "end":
21                 prev_op = stack.pop()
22                 op_duration = task.time - prev_op.end + 1
23                 res[task.id] += op_duration - prev_op.duration
24                 if stack:
25                     stack[-1].duration += op_duration
26         return res
27 
28 
29 print(Solution().exclusiveTime(3, ["0:start:0", "1:start:2", "2:start:3", "2:end:4", "1:end:5", "0:end:6"]))