Skip to content

Commit d8d5c14

Browse files
committed
Add solution #636
1 parent 7d10935 commit d8d5c14

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@
478478
629|[K Inverse Pairs Array](./0629-k-inverse-pairs-array.js)|Hard|
479479
630|[Course Schedule III](./0630-course-schedule-iii.js)|Hard|
480480
633|[Sum of Square Numbers](./0633-sum-of-square-numbers.js)|Medium|
481+
636|[Exclusive Time of Functions](./0636-exclusive-time-of-functions.js)|Medium|
481482
637|[Average of Levels in Binary Tree](./0637-average-of-levels-in-binary-tree.js)|Easy|
482483
643|[Maximum Average Subarray I](./0643-maximum-average-subarray-i.js)|Easy|
483484
645|[Set Mismatch](./0645-set-mismatch.js)|Medium|

Diff for: solutions/0636-exclusive-time-of-functions.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* 636. Exclusive Time of Functions
3+
* https://leetcode.com/problems/exclusive-time-of-functions/
4+
* Difficulty: Medium
5+
*
6+
* On a single-threaded CPU, we execute a program containing n functions. Each function has a
7+
* unique ID between 0 and n-1.
8+
*
9+
* Function calls are stored in a call stack: when a function call starts, its ID is pushed
10+
* onto the stack, and when a function call ends, its ID is popped off the stack. The function
11+
* whose ID is at the top of the stack is the current function being executed. Each time a
12+
* function starts or ends, we write a log with the ID, whether it started or ended, and the
13+
* timestamp.
14+
*
15+
* You are given a list logs, where logs[i] represents the ith log message formatted as a string
16+
* "{function_id}:{"start" | "end"}:{timestamp}". For example, "0:start:3" means a function call
17+
* with function ID 0 started at the beginning of timestamp 3, and "1:end:2" means a function
18+
* call with function ID 1 ended at the end of timestamp 2. Note that a function can be called
19+
* multiple times, possibly recursively.
20+
*
21+
* A function's exclusive time is the sum of execution times for all function calls in the program.
22+
* For example, if a function is called twice, one call executing for 2 time units and another
23+
* call executing for 1 time unit, the exclusive time is 2 + 1 = 3.
24+
*
25+
* Return the exclusive time of each function in an array, where the value at the ith index
26+
* represents the exclusive time for the function with ID i.
27+
*/
28+
29+
/**
30+
* @param {number} n
31+
* @param {string[]} logs
32+
* @return {number[]}
33+
*/
34+
var exclusiveTime = function(n, logs) {
35+
const stack = [];
36+
const result = new Array(n).fill(0);
37+
let previousTime = 0;
38+
39+
for (const log of logs) {
40+
const [id, action, time] = log.split(':');
41+
const currentTime = +time;
42+
43+
if (action === 'start') {
44+
if (stack.length) {
45+
result[stack[stack.length - 1]] += currentTime - previousTime;
46+
}
47+
stack.push(+id);
48+
previousTime = currentTime;
49+
} else {
50+
result[stack.pop()] += currentTime - previousTime + 1;
51+
previousTime = currentTime + 1;
52+
}
53+
}
54+
55+
return result;
56+
};

0 commit comments

Comments
 (0)