FastPrepFastPrep
Problem Brief

Get Function Execution Time

OA

When multiple tasks are included in a single thread/CPU, the tasks are scheduled based on the principle of preemption. When a higher priority task arrives, the currently executing task with lower priority gets preempted, i.e., it becomes idle until the higher priority task is complete.

There are 3 functions to be executed in a single thread with IDs between 0 and n-1. Given an integer n representing the number of functions to be executed, and an array logs containing the timestamp and function ID, determine the exclusive time of each of the functions. It is noted that the total of execution time should be divided among functions according to their representing an integer log in the form {function_id, "start"|"end", timestamp} indicating when a function started or ended at a time identified by the timestamp value.

Note: While calculating the execution time of a function both the starting and ending times of the function calls are to be included. The log of the function calls is sorted by timestamp. When a function is preempted at the beginning of timestamp second, the log of this time stamp is {"preempting_id", "start", timestamp}. When a function function_id is preempted after completing timestamp second, the log record is {"after_timestamp_record"}.

Function Description

Complete the function getFunctionExecutionTime in the editor.

getFunctionExecutionTime has the following parameters:

  1. 1. int n: the number of functions to be executed
  2. 2. String logs[m]: the execution logs of the different calls to the functions

Returns

int[n]: the execution time of all functions with IDs [0,n-1]

💐, Aura Man with the hard carry!༊·°🌺𓍼ོ

1Example 1

Input
n = 3, logs = ["0:start:0", "2:start:4", "2:end:5", "1:start:7", "1:end:10", "0:end:11"]
Output
[6, 4, 2]
Explanation
Example 1 illustration
Suppose n = 3, logs = ["0:start:0", "2:start:4", "2:end:5", "1:start:7", "1:end:10", "0:end:11"] Thus the total number of seconds allocated to functions 0, 1, and 2 is 6, 4, and 2 respectively. Hence the answer is [6, 4, 2].

Constraints

Limits and guarantees your solution can rely on.

  • 1 ≤ n ≤ 100
  • 1 ≤ m ≤ 500
  • 0 ≤ function_id < n
  • 0 ≤ timestamp ≤ 3*10^3
  • The timestamps are given in non-decreasing order.
  • No two starting timestamps and no two ending timestamps are equal
  • Every function "start" call has a corresponding "end" call
public int[] getFunctionExecutionTime(int n, String[] logs) {
  // write your code here
}
Input

n

3

logs

["0:start:0", "2:start:4", "2:end:5", "1:start:7", "1:end:10", "0:end:11"]

Output

[6, 4, 2]

Sign in to submit your solution.