Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find the exclusive time of these functions. 

Each function has a unique id, start from 0 to n-1. A function may be called recursively or by another function.

A log is a string has this format : function_id:start_or_end:timestamp. For example, "0:start:0" means function 0 starts from the very beginning of time 0. "0:end:0" means function 0 ends to the very end of time 0. 

Exclusive time of a function is defined as the time spent within this function, the time spent by calling other functions should not be considered as this function's exclusive time. You should return the exclusive time of each function sorted by their function id.

Example 1:

Input:
n = 2
logs = 
["0:start:0",
 "1:start:2",
 "1:end:5",
 "0:end:6"]
Output:[3, 4]
Explanation:
Function 0 starts at time 0, then it executes 2 units of time and reaches the end of time 1. 
Now function 0 calls function 1, function 1 starts at time 2, executes 4 units of time and end at time 5.
Function 0 is running again at time 6, and also end at the time 6, thus executes 1 unit of time. 
So function 0 totally execute 2 + 1 = 3 units of time, and function 1 totally execute 4 units of time.



Note:

Input logs will be sorted by timestamp, NOT log id.
Your output should be sorted by function id, which means the 0th element of your output corresponds to the exclusive time of function 0.
Two functions won't start or end at the same time.
Functions could be called recursively, and will always end.
1

Difficulty: MEDIUM_HARD
Tags: Data structures
Skill types: Data structures

Starter code:
class Solution:
    def exclusiveTime(self, n: int, logs: List[str]) -> List[int]:
        

Solutions:
class Solution:

	def exclusiveTime(self, n, logs):
		fn_stk = []
		excl_time = [0] * n
		prev = 0
		if n < 1 or not logs:
			return fn_stk
		(fn, action, ts) = logs[0].split(':')
		fn_stk.append(int(fn))
		for log in logs[1:]:
			(fn, action, ts) = log.split(':')
			(fn, ts) = (int(fn), int(ts))
			if action == 'start':
				if fn_stk:
					excl_time[fn_stk[len(fn_stk) - 1]] += ts - prev
				prev = ts
				fn_stk.append(fn)
			else:
				fid = fn_stk.pop()
				excl_time[fid] += ts - prev + 1
				prev = ts + 1
		return excl_time


class Solution:

	def exclusiveTime(self, n, logs):
		(f, idd, last, res) = ([], -99999, 0, [0] * n)
		for s in logs:
			(a, time, b) = s.split(':')
			(a, b) = (int(a), int(b))
			if time == 'start':
				f.append(a)
				if idd != -99999:
					res[idd] += b - last
				idd = a
				last = b
			else:
				f.pop()
				res[idd] += b - last + 1
				last = b + 1
				idd = f[-1] if f else -99999
		return res


class Solution:

	def exclusiveTime(self, n, logs):
		result = [0 for _ in range(n)]
		stack = []
		for log in logs:
			(func, op, time) = log.split(':')
			func = int(func)
			time = int(time)
			if op == 'start':
				if stack:
					(func_last, time_last) = stack[-1]
					result[func_last] += time - time_last
				stack.append([func, time])
			else:
				(func_last, time_last) = stack.pop()
				result[func_last] += time - time_last + 1
				if stack:
					stack[-1][1] = time + 1
		return result


class Solution:

	def exclusiveTime(self, n, logs):
		(status_map, spent_time, stack, cur_scope, pre_status, pre_timetamp) = ({'start': 1, 'end': 0}, {}, [], None, None, 0)
		for log in logs:
			(id, status, timestamp) = log.split(':')
			(id, timestamp) = (int(id), int(timestamp))
			if cur_scope is not None:
				diff = status_map[pre_status] - status_map[status]
				spent_time[cur_scope] = spent_time.get(cur_scope, 0) + timestamp - pre_timetamp + diff
			if status == 'start':
				stack.append(cur_scope)
				cur_scope = id
			else:
				cur_scope = stack.pop()
			(pre_timetamp, pre_status) = (timestamp, status)
		return [spent_time.get(id, 0) for id in range(n)]


class Solution:

	def exclusiveTime(self, n, logs):
		s = []
		a = [0] * n
		pop = False
		for log in logs:
			l = log.split(':')
			if l[1] == 'start' and len(s) > 0:
				a[s[-1]] += int(l[2]) - prev
				s.append(int(l[0]))
				prev = int(l[2])
				pop = False
			elif l[1] == 'start':
				s.append(int(l[0]))
				pop = False
				prev = int(l[2])
			elif l[1] == 'end':
				a[s[-1]] += int(l[2]) - prev + 1
				s.pop()
				pop = True
				prev = int(l[2]) + 1
		return a


class Solution:

	def exclusiveTime(self, n, logs):
		dic = {}
		stack = []
		for log in logs:
			l = log.split(':')
			if l[1] == 'start':
				if stack:
					stack[-1][1] += int(l[2]) - stack[-1][2]
					stack[-1][2] = -1
					stack += ([int(l[0]), 0, int(l[2])],)
				else:
					stack += ([int(l[0]), 0, int(l[2])],)
			else:
				(proc, cumuTime, start) = stack.pop()
				dic[proc] = dic.get(proc, 0) + (int(l[2]) - start + 1) + cumuTime
				if stack:
					stack[-1][2] = int(l[2]) + 1
		return [dic[k] for k in sorted(dic)]


class Solution:

	def exclusiveTime(self, n, logs):
		dict = {}
		st = []
		ans = []
		for i in range(n):
			dict[i] = 0
		for s in logs:
			if s.split(':')[1] == 'start':
				if not st:
					st.append([int(s.split(':')[0]), int(s.split(':')[2])])
				else:
					time = int(s.split(':')[2]) - st[-1][1]
					dict[st[-1][0]] += time
					st.append([int(s.split(':')[0]), int(s.split(':')[2])])
			else:
				time = int(s.split(':')[2]) - st[-1][1]
				dict[st[-1][0]] += time + 1
				st.pop()
				if st:
					st[-1][1] = int(s.split(':')[2]) + 1
		for i in range(n):
			ans += [dict[i]]
		return ans


class Solution:

	def exclusiveTime(self, n, logs):
		spent = [0 for i in range(n)]
		i = 0
		while len(logs) > 2:
			s1 = logs[i].split(':')
			s2 = logs[i + 1].split(':')
			if s1[1] == 'start' and s2[1] == 'end' and (s1[0] == s2[0]):
				spent[int(s1[0])] += int(s2[2]) - int(s1[2]) + 1
				if i > 0:
					spent[int(logs[i - 1].split(':')[0])] -= int(s2[2]) - int(s1[2]) + 1
				if i == 0:
					logs = logs[2:]
				else:
					logs = logs[0:i] + logs[i + 2:]
					i -= 1
			else:
				i += 1
		spent[int(logs[0].split(':')[0])] += int(logs[1].split(':')[2]) - int(logs[0].split(':')[2]) + 1
		return spent