deno.land / x / masx200_leetcode_test@10.6.5 / exclusive-time-of-functions / index.ts

نووسراو ببینە
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
34
35
36
37
38
39
40
41
42
export default function exclusiveTime(n: number, logs: string[]): number[] { const ans: number[] = Array(n).fill(0); const stack: [number, number][] = []; const data = logs.map((log) => { const [q, w, e] = log.split(":"); return [ Number(q), Number(w === "start"), Number(e) + Number(w !== "start"), ]; }); //console.log(data) for (const [a, b, c] of data) { if (stack.length) { const last = stack[stack.length - 1];
if (b) { ans[last[0]] += c - last[1]; stack.push([a, c]); } else { ans[a] += c - last[1];
stack.pop(); if (stack.length) { stack[stack.length - 1][1] = c; } } } else { if (b) { stack.push([a, c]); } else { throw new Error("first end"); } } // console.log(stack) } if (stack.length) { throw Error("not end"); } return ans;}
masx200_leetcode_test

Version Info

Tagged at
a year ago