LeetCode 636: Exclusive Time of Functions Solution

Master LeetCode problem 636 (Exclusive Time of Functions), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

636. Exclusive Time of Functions

Problem Explanation

Explanation

To solve this problem, we can simulate the execution of functions using a stack to keep track of the start and end times of each function. We iterate through the logs and update the exclusive time for each function accordingly.

Here's the algorithm:

  1. Initialize an array exclusiveTime of size n to store the exclusive time for each function.
  2. Initialize a stack stack to keep track of functions.
  3. Iterate through each log:
    • Split the log into function ID, event (start/end), and timestamp.
    • If the event is a start event:
      • If the stack is not empty, update the exclusive time of the function at the top of the stack.
      • Push the function ID and timestamp onto the stack.
    • If the event is an end event:
      • Update the exclusive time of the function at the top of the stack.
      • Pop the function ID and timestamp from the stack.
  4. Return the exclusiveTime array.

Time complexity: O(logs.length) Space complexity: O(n)

Solution Code

class Solution {
    public int[] exclusiveTime(int n, List<String> logs) {
        int[] exclusiveTime = new int[n];
        Stack<int[]> stack = new Stack<>();

        for (String log : logs) {
            String[] parts = log.split(":");
            int functionId = Integer.parseInt(parts[0]);
            String event = parts[1];
            int timestamp = Integer.parseInt(parts[2]);

            if (event.equals("start")) {
                if (!stack.isEmpty()) {
                    int[] prev = stack.peek();
                    exclusiveTime[prev[0]] += timestamp - prev[1];
                }
                stack.push(new int[]{functionId, timestamp});
            } else {
                int[] current = stack.pop();
                exclusiveTime[functionId] += timestamp - current[1] + 1;

                if (!stack.isEmpty()) {
                    stack.peek()[1] = timestamp + 1;
                }
            }
        }

        return exclusiveTime;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 636 (Exclusive Time of Functions)?

This page provides optimized solutions for LeetCode problem 636 (Exclusive Time of Functions) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 636 (Exclusive Time of Functions)?

The time complexity for LeetCode 636 (Exclusive Time of Functions) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 636 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 636 in Java, C++, or Python.

Back to LeetCode Solutions