Advertisement

Exclusive Time of Functions - LeetCode 636 Solution

Exclusive Time of Functions - Complete Solution Guide

Exclusive Time of Functions is LeetCode problem 636, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

On a single-threaded CPU, we execute a program containing n functions. Each function has a unique ID between 0 and n-1 . Function calls are stored in a call stack : when a function call starts, its ID is pushed onto the stack, and when a function call ends, its ID is popped off the stack. The function whose ID is at the top of the stack is the current function being executed . Each time a function starts or ends, we write a log with the ID, whether it started or ended, and the timestamp. You are

Detailed Explanation

The problem asks us to calculate the exclusive time of each function in a single-threaded program given a log of function calls. Each log entry indicates whether a function started or ended at a specific timestamp. The exclusive time for a function is the total time it spent actively executing (excluding time spent while another function was called recursively). We need to return an array where the i-th element represents the exclusive time of the function with ID i.

Solution Approach

The solution uses a stack to maintain the order of function calls. We iterate through the logs, parsing each log entry to determine the function ID, event type (start or end), and timestamp. If it's a 'start' event, we check if a function is already running. If so, we add the time elapsed to that function's exclusive time. Then, we push the current function ID onto the stack. If it's an 'end' event, we calculate the exclusive time of the function that just ended, pop it from the stack, and update the previous timestamp.

Step-by-Step Algorithm

  1. Step 1: Initialize an array `exclusive_times` of size `n` with all elements set to 0. This array will store the exclusive time for each function.
  2. Step 2: Initialize an empty stack `stack` to keep track of the currently executing function IDs.
  3. Step 3: Initialize a variable `prev_time` to 0 to store the timestamp of the previous log entry.
  4. Step 4: Iterate through the `logs` array.
  5. Step 5: For each log entry, split the string by ':' to extract the function ID (`fid`), event type (`event`), and timestamp (`timestamp`). Convert `fid` and `timestamp` to integers.
  6. Step 6: If the `event` is 'start':
  7. Step 6a: If the `stack` is not empty, calculate the time elapsed since the previous event (`timestamp - prev_time`) and add it to the exclusive time of the function at the top of the `stack` (`exclusive_times[stack[-1]]`).
  8. Step 6b: Push the current `fid` onto the `stack`.
  9. Step 6c: Update `prev_time` to the current `timestamp`.
  10. Step 7: If the `event` is 'end':
  11. Step 7a: Calculate the exclusive time of the function that just ended (`timestamp - prev_time + 1`) and add it to its corresponding entry in `exclusive_times` (`exclusive_times[stack.pop()]`).
  12. Step 7b: Pop the function ID from the `stack`.
  13. Step 7c: Update `prev_time` to `timestamp + 1`.
  14. Step 8: After iterating through all log entries, return the `exclusive_times` array.

Key Insights

  • Insight 1: The problem can be solved efficiently using a stack to keep track of the currently executing function(s). The stack helps manage nested function calls and track execution time.
  • Insight 2: When a 'start' event occurs, we need to check if another function is already running (i.e., the stack is not empty). If so, we add the time elapsed since the last event to the running function's exclusive time.
  • Insight 3: When an 'end' event occurs, we calculate the exclusive time of the function that just ended, taking into account the time elapsed and the inclusive nature of the 'end' timestamp. We also need to update the 'previous time' accordingly to account for the end event.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Stack.

Companies

Asked at: IBM, LinkedIn, Snap, athenahealth.