3. Question 3 There is a dual-core processor with one process queue for each core. There…
- User
- Question 3
There is a dual-core processor with one process queue for each core. There are 𝑛 n processes, where the time to process the 𝑖 i-th process is denoted by time [ 𝑖 ] time[i] (1 ≤ 𝑖 i ≤ 𝑛 n). There is a latch that helps decide which process is processed by which core.
Initially, the first core has the latch. Suppose the latch is currently with the 𝑐 c-th core, where 𝑐 c is either 1 or 2, and the 𝑖 i-th process needs to be assigned. Then one of the following operations must be performed:
The 𝑖 i-th process is assigned to the core 𝑐 c, and the latch is given to the other core. The 𝑖 i-th process is assigned to the other core, and the latch is retained by the core 𝑐 c. The aim of each core is to have a maximum sum of time of processes with them for better performance. So, while assigning the 𝑖 i-th process, the core with the latch decides the operation to be performed such that the total sum of time of processes assigned to it is maximized after all the processes are assigned.
Find the sum of the time of processes assigned to the first and second cores if both the cores work optimally. Return an array of two integers, where the first integer is the sum of the times of processes assigned to the first core, and the second integer is the sum of the times of processes assigned to the second core.
Example
Given 𝑛
5 n=5 and time
[ 10 , 21 , 10 , 21 , 10 ] time=[10,21,10,21,10]:
Latch Time Assigned Core New Latch 1 10 2nd 1 2 21 1st 2 1 10 2nd 1 2 21 1st 2 1 10 2nd 1 Hence, the total time of processes for the first core is ( 21 + 10 + 21 )
52 (21+10+21)=52, and the total time of processes for the second core is ( 10 + 10 )
20 (10+10)=20. The given assignment is not optimal, as for the 4th process, the 2nd core assigned the process with process time 21 21 to core 1 and got the 5th process with process time 10 10, which is not an optimal move by core 2.
The optimal move by the 2nd core would have been to assign the 4th process to itself and the 5th process to core 1; this would have given the total time for the 2nd core = 31, which is better than 20.
Return the answer array: [ 41 , 31 ] [41,31].
Function Description
Complete the function getProcessTime in the editor below.
getProcessTime has the following parameter:
int time[]: the processing time required for each process. Returns
long[2]: the sums of the processing times of the first and second cores. Constraints
1 ≤ 𝑛 ≤ 1 0 5 1≤n≤10 5
1 ≤ time [ 𝑖 ] ≤ 1 0 5 1≤time[i]≤10 5
give python code
- Assistant