share my Python stack based solution beats 100%


  • 0
    N
    class Solution(object):
    
        def exclusiveTime(self, n, logs):
            """
            :type n: int
            :type logs: List[str]
            :rtype: List[int]
            """
            stack = []
            time = [0] * n
            for log in logs:
                log_id, log_type, t = log.split(':')
                log_id, t = int(log_id), int(t)
                if log_type == 'start':
                    stack.append((log_id, t))
                else:
                    log_id, s_t = stack.pop()
                    duration = t - s_t + 1
                    # add this duration to current function call
                    time[log_id] += duration
                    # subtract this duration from caller's total time
                    if stack:
                        time[stack[-1][0]] -= duration
            return time
    

Log in to reply
 

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