636. Exclusive Time of Functions

Information

  • Diffculty: Medium

  • Created: 2019-09-01 18:11:18

  • Last Motified: 2019-09-01 18:11:18

Solution

class Solution:
    def exclusiveTime(self, n: int, logs: List[str]) -> List[int]:
        totalTime = [0] * n
        stack = []
        last = 0
        current = 0
        for l in logs:
            func, action, ts = l.split(':')
            func, ts = int(func), int(ts)
            if action == 'end':
                ts += 1
            current += ts - last
            if action == 'start':
                stack.append(current)
                current = 0
            else:
                totalTime[func] += current
                current = stack.pop()
            last = ts
        return totalTime