Horje
Find Exclusive Time of Functions

Given a list logs, where logs[i] represents the ith log message formatted as a string “{function_id}:{“start” | “end”}:{timestamp}”. For example, “0:start:3” means a function call with function ID 0 started at the beginning of timestamp 3, and “1:end:2” means a function call with function ID 1 ended at the end of timestamp 2. Note that a function can be called multiple times, possibly recursively.

A function’s exclusive time is the sum of execution times for all function calls in the program. For example, if a function is called twice, one call executing for 2 time units and another call executing for 1 time unit, the exclusive time is 2 + 1 = 3.

Return the exclusive time of each function in an array, where the value at the ith index represents the exclusive time for the function with ID i.

Examples:

Input: n = 2, logs[] = {“0:start:0”, “1:start:2”, “1:end:5”, “0:end:6”}
Output: [3,4]
Explanation:

  • Function 0 starts at the beginning of time 0, then it executes 2 for units of time and reaches the end of time 1.
  • Function 1 starts at the beginning of time 2, executes for 4 units of time, and ends at the end of time 5.
  • Function 0 resumes execution at the beginning of time 6 and executes for 1 unit of time.
  • So function 0 spends 2 + 1 = 3 units of total time executing, and function 1 spends 4 units of total time executing.

Input: n = 1, logs[] = {“0:start:0”, “0:start:2”, “0:end:5”, “0:start:6”, “0:end:6”, “0:end:7”}
Output: [8]
Explanation:

  • Function 0 starts at the beginning of time 0, executes for 2 units of time, and recursively calls itself.
  • Function 0 (recursive call) starts at the beginning of time 2 and executes for 4 units of time.
  • Function 0 (initial call) resumes execution then immediately calls itself again.
  • Function 0 (2nd recursive call) starts at the beginning of time 6 and executes for 1 unit of time.
  • Function 0 (initial call) resumes execution at the beginning of time 7 and executes for 1 unit of time.
  • So function 0 spends 2 + 4 + 1 + 1 = 8 units of total time executing.

Approach: To solve the problem, follow the below idea:

The idea is simple every time we see a start, we just push it to the stack. Now when we reach an end, we are guaranteed that the top of the stack is a start with the same id as the current item because all completed start/ends in between this start and end has been removed already. We just add current item timestamp – stack top timestamp + 1 to times[i].

For example:

[…, {0:start:3}] and item = {0:end:6} we add 6 – 3 + 1

However, what if there are function calls in between the start and end of the function of id 0? We can account for this by subtracting the length of the function calls in between the function id 0 whenever we complete an inner function marked by an end.

[…, {0:start:3}, {2:start:4}] and item = {2:end:5} so we increment times[2] by curr_length = 5 – 4 + 1 = 2 and then we subtract times[0] by curr_length as it takes up that amount of time out of the total time

So whenever we see an end, we have to make sure to subtract our curr_length to whatever function is enclosing it if it exists.

Step-by-step algorithm:

  • Create a vector ‘times‘ to store the exclusive time of each function.
  • Create a stack ‘st‘ to store the log entries.
  • Iterate through the ‘logs‘ array:
    • If the current log entry is a ‘start’ entry:
      • Push the log entry onto the stack.
    • If the current log entry is an ‘end’ entry:
      • Retrieve the previous ‘start’ log entry from the top of the stack.
      • Calculate the time difference between the ‘end’ and ‘start’ entries, and add it to the ‘times’ vector for the corresponding function.
      • Remove the ‘start’ log entry from the stack.
      • If the stack is not empty, subtract the time difference from the ‘times’ vector for the previous function.
  • Return the ‘times’ vector.

Below is the implementation of the algorithm:

C++
#include <bits/stdc++.h>
using namespace std;

// Structure to store log information
struct Log {
    int id;
    string status;
    int timestamp;
};

// Function to calculate the exclusive time of n
// functions
vector<int> exclusiveTime(int n, vector<string>& logs)
{
    // Initialize the times vector with 0
    vector<int> times(n, 0);
    // Create a stack to store the logs
    stack<Log> st;

    // Iterate through the logs
    for (string log : logs) {
        stringstream ss(log);
        string temp, temp2, temp3;
        getline(ss, temp, ':');
        getline(ss, temp2, ':');
        getline(ss, temp3, ':');

        Log item = { stoi(temp), temp2, stoi(temp3) };
        // If the current log is a start log
        if (item.status == "start") {

            // Push the log onto the stack
            st.push(item);
        }
        else { // If the current log is an end log

            // Calculate the time added for the current
            // function
            int time_added
                = item.timestamp - st.top().timestamp + 1;

            // Add the time to the times vector
            times[st.top().id] += time_added;
            // Pop the log from the stack
            st.pop();

            // If there are still logs on the stack
            if (!st.empty()) {
                // Subtract the time from the previous
                // function on the stack
                times[st.top().id] -= time_added;
            }
        }
    }
    // Return the times vector
    return times;
}

// Driver code
int main()
{
    int n = 2;
    vector<string> logs = { "0:start:0", "1:start:2",
                            "1:end:5", "0:end:6" };

    vector<int> result = exclusiveTime(n, logs);
    for (int i : result) {
        cout << i << " ";
    }
    cout << endl;
    return 0;
}

Output
3 4 

Time complexity: O(n), where n is the number of log entries, as we iterate through the ‘logs’ vector once.
Space Complexity: O(n), where n is the number of functions and log entries.




Reffered: https://www.geeksforgeeks.org


DSA

Related
Minimum operations to complete all tasks Minimum operations to complete all tasks
Minimum Time Required to Visit Each Disappearing Nodes Minimum Time Required to Visit Each Disappearing Nodes
The Ultimate Interview Preparation Roadmap for Software Developers The Ultimate Interview Preparation Roadmap for Software Developers
Minimum rectangles to cover all points Minimum rectangles to cover all points
Find maximum in stack in O(1) without using additional stack in Python Find maximum in stack in O(1) without using additional stack in Python

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
11