Ctrl K

Own Time per Function

Medium
A single-threaded program calls count functions numbered from 0 to count - 1. Calls form a stack: at any moment the function on top of the stack runs, while its caller waits. Time is split into units numbered 0, 1, 2, … The list logs holds entries of the form "id:start:t" and "id:end:t" in chronological order. A start at t means the call began at the beginning of unit t; an end at t means the call finished at the end of unit t. A function may be called many times, recursion included. A function's own time is the number of time units during which it was running itself, that is, sitting on top of the stack, across all of its calls. Return a list of length count whose i-th entry is the own time of function i.
Example 1
count=2, logs=["0:start:0", "1:start:3", "1:end:4", "0:end:8"] → [7, 2]

Function 0 runs during units 0–2, then function 1 runs during units 3 and 4, then function 0 again during units 5–8. That gives 3 + 4 = 7 and 2.

Example 2
count=3, logs=["2:start:1", "2:end:1", "0:start:2", "0:start:4", "0:end:4", "0:end:5"] → [4, 0, 1]

Function 2 takes exactly one unit (unit 1). Function 0 calls itself: the outer call runs during units 2, 3 and 5, the inner one during unit 4, 4 in total. Function 1 is never called.

Constraints
  • 1 ≤ count ≤ 100
  • 1 ≤ len(logs) ≤ 500
  • 0 ≤ function id < count
  • 0 ≤ t ≤ 10⁹
  • The log is consistent: every start has its end, calls nest properly, and entries go in time order
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPostfix Calculator MediumPreorder of a Search Tree MediumLow, Peak, Middle MediumSmallest Up-Down Sequence MediumNext Larger Around the Circle Medium
loading Python…
Input
count=2, logs=["0:start:0", "1:start:3", "1:end:4", "0:end:8"]
Expected
[7, 2]

Run checks the first 3 tests in your browser. Submit checks all 43 tests on the server, hidden ones included.