bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 636. Exclusive Time of Functions

636. Exclusive Time of Functions

Leetcode Solutions

Using Stack to Track Function Execution Times

  1. Initialize an array res of length n to store the exclusive time for each function, all initialized to 0.
  2. Initialize a stack to keep track of active function calls.
  3. Initialize a variable prev_time to keep track of the start of the current interval.
  4. Iterate through the logs list, and for each log entry: a. Parse the log entry to extract the function ID, action (start or end), and timestamp. b. If the action is 'start': i. If the stack is not empty, update the exclusive time of the function at the top of the stack. ii. Push the current function ID onto the stack. iii. Update prev_time to the current timestamp. c. If the action is 'end': i. Pop the function ID from the top of the stack. ii. Update the exclusive time of the popped function. iii. Update prev_time to one plus the current timestamp (since the end time is inclusive).
  5. Return the res array containing the exclusive times.
UML Thumbnail

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...