![]() |
VOOZH | about |
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:
Below is the implementation of the algorithm:
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.