618. Exclusive Time of Functions

MediumStackStackSimulation

A single-threaded CPU runs functions logged as 'id:start:time' or 'id:end:time'. Given n functions and the logs in order, return the exclusive time of each function — the total time spent in it, excluding time spent in nested calls. The input is JSON {n, logs}.

Input: JSON {n, logs}.

Output: Array — the exclusive time per function id.

Examples

Example 1
Input: {"n":2,"logs":["0:start:0","1:start:2","1:end:5","0:end:6"]}
Output: [3,4]
Explanation: Function 1 runs 4 units; function 0 runs 3 excluding the nested call.
Example 2
Input: {"n":1,"logs":["0:start:0","0:end:0"]}
Output: [1]
Explanation: One unit of time.
Example 3
Input: {"n":1,"logs":["0:start:0","0:end:4"]}
Output: [5]
Explanation: Five inclusive units.

Constraints

Asked by

MetaIBMAmazonGoogleAppleMicrosoft
Solve this problem in the editor →