A single-threaded CPU runs n functions, numbered 0 to n − 1. Calls nest like a call stack: when a function calls another, the caller pauses until the callee returns. A function may call itself.
You get the run as logs, in time order. Each entry is "id:start:t" or "id:end:t". A start at t means the call begins at the very start of time unit t; an end at t means it finishes at the very end of unit t, so that unit belongs to it.
A function's exclusive time is the number of units during which it was the one actually running, summed over all its calls. Return an array whose entry i is the exclusive time of function i.
Example 1
Input: n = 3, logs = ["0:start:0", "1:start:2", "2:start:3",
"2:end:4", "1:end:6", "0:end:9"]
Output: [5, 3, 2]
Why: f0 runs 0–1 and 7–9, f1 runs 2 and 5–6, f2 runs 3–42 more examples and the constraints
Example 2
Input: n = 1, logs = ["0:start:1", "0:start:3", "0:end:3", "0:end:5"] Output: [5] Why: the outer call runs 1–2 and 4–5, the inner one runs 3
Example 3
Input: n = 2, logs = ["0:start:1", "1:start:3", "1:end:4", "0:end:7"] Output: [5, 2] Why: f1 owns units 3 and 4; f0 owns 1–2 and 5–7
Constraints
- n is between 1 and 100; logs has between 2 and 500 entries
- 0 ≤ id < n and 0 ≤ t ≤ 1,000,000,000
- No two starts share a timestamp, and no two ends share a timestamp
- Every start has a matching end, and the logs describe a valid nesting
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Span minus child timeO(L)
- Stack with a running clockO(L)
The idea in one sentence
Side by side
| Approach | Time | Relative work for 500 logs | Verdict |
|---|---|---|---|
| Span minus child time | O(L) | Works; keeps three numbers per call | |
| Stack with a running clock | O(L) | Best; one id per call |
L = number of logs. Time does not depend on the timestamps, which can be up to 10^9.
Edge cases to test
- Call of one unit.
0:start:5then0:end:5is 1 unit, not 0: the end unit counts. - Recursion. The same id can be on the stack several times; each frame is credited separately to the same total.
- Function never called. Its entry stays 0.
- Back-to-back calls. An end at 2 and a start at 3 leave no gap: prev is already 3.
- Huge timestamps. Times reach 10^9, so never loop over time units; work only per log.
Common mistakes
- Adding
t − prevon an end, which drops the unit the call ends in. - Setting
prev = tafter an end instead oft + 1, which credits the end unit twice. - Simulating unit by unit: with timestamps up to 10^9 that never finishes.
How to talk through it
- 1Clarify. Is an end inclusive of its unit? Can a function call itself? Are logs sorted by time?
- 2Name the structure. Calls nest, so a stack of open calls tells me who is running.
- 3Give the first version. Credit each call its span minus its children's spans when it ends.
- 4Simplify. Credit time between consecutive events to the top of the stack, tracking prev.
- 5Test out loud. Run a one-unit call, a recursive call, and two calls back to back.