-
Notifications
You must be signed in to change notification settings - Fork 1
/
0636_exclusiveTimeOfFunctions.js
33 lines (30 loc) · 1.1 KB
/
0636_exclusiveTimeOfFunctions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* @param {number} n Number of unique functions
* @param {string[]} logs Function execution logs
* @return {number[]} Time each function spent executing.
* @summary Exclusive Time of Functions {@link https://leetcode.com/problems/exclusive-time-of-functions/}
* @description Given number of unique functions and their execution logs, return time each function spent executing.
* Space O(n) - Where n is number of unique functions.
* Time O(l) - Where l is number of log entries.
*/
const exclusiveTime = (n, logs) => {
const stack = [];
const response = Array(n).fill(0);
let previousTime = 0;
for (let index = 0; index < logs.length; index++) {
let [process, instruction, time] = logs[index].split(':');
process = +process;
time = +time;
const stackTop = stack[stack.length - 1];
if (instruction === 'start') {
if (stack.length) response[stackTop] += time - previousTime;
stack.push(process);
previousTime = time;
} else {
response[stackTop] += time - previousTime + 1;
stack.pop();
previousTime = time + 1;
}
}
return response;
};