-
Notifications
You must be signed in to change notification settings - Fork 1
/
exclusive-time-of-functions.py
33 lines (30 loc) · 1.17 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
class Solution:
def exclusiveTime(self, n, logs):
"""
:type n: int
:type logs: List[str]
:rtype: List[int]
"""
def process_log(line):
func_id, status, time = line.split(":")
return int(func_id), status , int(time)
from collections import defaultdict
sys_stack = []
time_counter = defaultdict(int)
for log in logs:
func_id, status, time = process_log(log)
if status == "start":
if not sys_stack:
pass
else:
# suspend last func
last_func_id, last_func_st_time = sys_stack[-1]
time_counter[last_func_id] += time - last_func_st_time
sys_stack.append([func_id, time])
elif status == "end":
last_func_id, last_func_st_time = sys_stack.pop()
assert last_func_id == func_id
time_counter[last_func_id] += time - last_func_st_time + 1
if sys_stack:
sys_stack[-1][1] = time + 1
return [time_counter[k] for k in sorted(time_counter.keys())]