1. Question 1
When multiple tasks are executed on a single-threaded CPU, the tasks are scheduled based on the principle of preemption. When a higher priority task arrives in the execution queue, then the lower-priority task is preempted, i.e., its execution is paused until the higher-priority task is complete.
There are n functions to be executed on a single-threaded CPU, with each function having a unique ID in the range of 0 to n-1. Given an integer n representing the number of functions and an array logs, representing execution logs as an array of strings, each of the m entries in the logs array is of the form <function_id>:<start|end>:<timestamp>. Each entry in the logs contains the exclusive times of each of the functions for all its execution. An entry <function_id>:<start> in the logs, representing an execution step, is of the form <function_id>:<start>:<timestamp>, indicating that the function with ID <function_id> starts or ends at a time identified by the timestamp value.
Note: While calculating the execution time of the function, if a function is preempted, the time of the function will have to be simulated. The log of the function contains start and end times of the function, denoted as <function_id>:<start>:<timestamp> and <function_id>:<end>:<timestamp> respectively. <function_id>:<end>:<timestamp> means that the function with ID <function_id> ends after completing the timestamp.
Example
Suppose n = 3, logs = ["0:start:0", "2:start:4", "2:end:5", "1:start:7", "1:end:10", "0:end:11"]
| Timestamp | Function Running | Remarks |
|---|---|---|
| 0 | 0 | Function 0 starts |
| 1 | 0 | |
| 2 | 0 | |
| 3 | 0 | |
| 4 | 2 | Function 2 starts and Function 0 is preempted |
| 5 | 2 | Function 2 ends |
| 6 | 0 | Function 0 resumes |
| 7 | 1 | Function 1 starts and Function 0 is preempted |
| 8 | 1 | |
| 9 | 1 | |
| 10 | 1 | Function 1 ends |
| 11 | 0 | Function 0 ends |
Thus the total number of seconds allocated to functions 0, 1, and 2 are 6, 4, and 2 respectively. Hence the answer is [6, 4, 2].
Function Description
Complete the function getTotalExecutionTime in the editor below.
getTotalExecutionTime has the following parameters:
int n: the number of functions to be executed.string logs[m]: the execution logs of the different calls to the functions.
Returns
int[n]: the execution time of all functions with IDs from 0 to n-1.
Constraints
1 ≤ n ≤ 1001 ≤ m ≤ 5000 ≤ function_id < n0 ≤ 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:startcall has a correspondingfunction:endcall.
我们长期稳定承接各大科技公司如TikTok、Google、Amazon等的OA笔试代写服务,确保满分通过。如有需求,请随时联系我们。
We consistently provide professional online assessment services for major tech companies like TikTok, Google, and Amazon, guaranteeing perfect scores. Feel free to contact us if you're interested.

