Exclusive Time of Functions
On a single-threaded CPU, n functions run with ids 0..n-1. You are given logs where each entry is "id:start:timestamp" or "id:end:timestamp". A function that starts preempts the currently running one (nested calls). Return an array where the i-th value is the exclusive time of function i — the total time it spent executing on the CPU, not counting time spent inside functions it called.
Open official problem prompt ↗Attribute each unit of CPU time to the exact function that was actually executing at that moment, excluding time spent inside nested calls.
Like a stopwatch that always times only the person currently speaking in a meeting: when someone interrupts, you note how long the previous speaker held the floor and start timing the interrupter.
- Input
- n = 2, logs = ["0:start:0", "1:start:2", "1:end:5", "0:end:6"]
- Output
- [3, 4]
- Why
- Function 0 runs at times 0-1 (2 units) and 6-6 (1 unit) for 3 total; function 1 runs 2-5 for 4 total.
1 <= n <= 1001 <= logs.length <= 5000 <= function id < n0 <= timestamp <= 10^9No two start events and no two end events happen at the same timestampEach function has a matching start/end pair and calls are properly nested