Python Stack Solution


  • 1
    class Solution(object):
        def exclusiveTime(self, n, logs):
            """
            :type n: int
            :type logs: List[str]
            :rtype: List[int]
            """
            ans = [0] * n
            stack = []
            for log in logs:
                uid, status, time = map(int, log.replace("start", "1").replace("end", "0").split(":"))
                if status:
                    stack.append([time, 0])
                else:
                    start, innerTime = stack.pop()
                    ans[uid] += time - start + 1 - innerTime
                    if stack:
                        stack[-1][1] += time - start + 1
            return ans
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.