Leetcode 636 Solution

This article provides solution to leetcode question 636 (exclusive-time-of-functions)

https://leetcode.com/problems/exclusive-time-of-functions

Solution

class Solution:
    def exclusiveTime(self, n: int, logs: List[str]) -> List[int]:
        m = collections.defaultdict(int)

        s = []
        for log in logs:
            func_id, op, t = log.split(':')
            t = int(t)
            func_id = int(func_id)

            if op == 'start':
                if s:
                    s[-1] = (None, s[-1][1] + t - s[-1][0])
                s.append((t, 0))
            else:
                m[func_id] += t - s[-1][0] + 1 + s[-1][1]
                s.pop(-1)

                if s:
                    s[-1] = (t + 1, s[-1][1])

        return [m[k] for k in sorted(m.keys())]