Untitled

 avatar
unknown
python
a year ago
868 B
7
Indexable
def getTotalExecutionTime(n, logs):
    execution_times = [0] * n
    stack = []
    prev_timestamp = 0

    for log in logs:
        function_id, action, timestamp = log.split(':')
        function_id = int(function_id)
        timestamp = int(timestamp)

        if action == 'start':
            if stack:
                execution_times[stack[-1]] += timestamp - prev_timestamp
            stack.append(function_id)
            prev_timestamp = timestamp
        else:  # 'end'
            execution_times[stack.pop()] += timestamp - prev_timestamp + 1
            prev_timestamp = timestamp + 1

    return execution_times

# Test the function with the given example
n = 3
logs = ["0:start:0", "2:start:4", "2:end:5", "1:start:7", "1:end:10", "0:end:11"]
result = getTotalExecutionTime(n, logs)
print(result)  # Expected output: [6, 4, 2]
Editor is loading...
Leave a Comment