-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathExclusive Time of Functions.py
35 lines (34 loc) · 1.44 KB
/
Exclusive Time of Functions.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
# Runtime: 78 ms (Top 92.98%) | Memory: 14.2 MB (Top 33.69%)
class Solution:
#T=O(n), S=O(d)
#n=len of logs, d=depth of stack
def exclusiveTime(self, n: int, logs: List[str]) -> List[int]:
#init result array to zeroes of length n (function ids)
res = [0]*n
stack = []
#iterate through logs
for log in logs:
#split the log
#function_id: start|end: timestamp
log = log.split(":")
#type cast function id and timestamp to int type
id = int(log[0])
timestamp = int(log[2])
state = log[1]
#detect start of a function call
#stack[function_id, start_timestamp]
if state == "start":
#stack is non empty
if stack:
#get the time taken by last function so far
res[stack[-1][0]] += timestamp - stack[-1][1]
#append the current function_id and start timestamp to the stack
stack.append([id, timestamp])
else:
#get the time consumed by current function
#dont forget to add 1 as the last unit of time should be included
res[id] += timestamp - stack.pop()[1] + 1
if stack:
#update the start time of last function in stack to get the cumulative result
stack[-1][1] = timestamp + 1
return res