Minimum cost to process m tasks where switching costs | Function to find out the farthest position where one of the currently ongoing tasks will rehappen . ; Iterate form last to current position and find where the task will happen next . ; Find out maximum of all these positions and it is the farthest position . ; Function to find out minimum cost to process all the tasks ; freqarr [ i ] [ j ] denotes the frequency of type j task after position i like in array 1 , 2 , 1 , 3 , 2 , 1 frequency of type 1 task after position 0 is 2. So , for this array freqarr [ 0 ] [ 1 ] = 2. Here , i can range in between 0 to m - 1 and j can range in between 0 to m ( though there is not any type 0 task ) . ; Fill up the freqarr vector from last to first . After m - 1 th position frequency of all type of tasks will be 0. Then at m - 2 th position only frequency of arr [ m - 1 ] type task will be increased by 1. Again , in m - 3 th position only frequency of type arr [ m - 2 ] task will be increased by 1 and so on . ; isRunning [ i ] denotes whether type i task is currently running in one of the cores . At the beginning no tasks are running so all values are false . ; cost denotes total cost to assign tasks ; truecount denotes number of occupied cores ; iterate through each task and find the total cost . ; ele denotes type of task . ; Case 1 : if same type of task is currently running cost for this is 0. ; Case 2 : same type of task is not currently running . ; Subcase 1 : if there is at least one free core then assign this task to that core at a cost of 1 unit . ; Subcase 2 : No core is free ; set minimum frequency to a big number ; set index of minimum frequency task to 0. ; find the minimum frequency task type ( miniind ) and frequency ( mini ) . ; If minimum frequency is zero then just stop the task and start the present task in that core . Cost for this is 1 unit . ; If minimum frequency is nonzero then find the farthest position where one of the ongoing task will rehappen . Stop that task and start present task in that core . ; find out the farthest position using find function ; return total cost ; Driver Code ; Test case 1 ; Test case 2 ; Test case 3
def find ( arr , pos , m , isRunning ) : NEW_LINE INDENT d = [ 0 ] * ( m + 1 ) NEW_LINE for i in range ( m - 1 , pos , - 1 ) : NEW_LINE INDENT if isRunning [ arr [ i ] ] : NEW_LINE INDENT d [ arr [ i ] ] = i NEW_LINE DEDENT DEDENT maxipos = 0 NEW_LINE for ele in d : NEW_LINE INDENT maxipos = max ( ele , maxipos ) NEW_LINE DEDENT return maxipos NEW_LINE DEDENT def mincost ( n , m , arr ) : NEW_LINE INDENT freqarr = [ [ ] for i in range ( m ) ] NEW_LINE newvec = [ 0 ] * ( m + 1 ) NEW_LINE freqarr [ m - 1 ] = newvec [ : ] NEW_LINE for i in range ( m - 2 , - 1 , - 1 ) : NEW_LINE INDENT nv = freqarr [ i + 1 ] [ : ] NEW_LINE nv [ arr [ i + 1 ] ] += 1 NEW_LINE freqarr [ i ] = nv [ : ] NEW_LINE DEDENT isRunning = [ False ] * ( m + 1 ) NEW_LINE cost = 0 NEW_LINE truecount = 0 NEW_LINE for i in range ( 0 , m ) : NEW_LINE INDENT ele = arr [ i ] NEW_LINE if isRunning [ ele ] == True : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT if truecount < n : NEW_LINE INDENT cost += 1 NEW_LINE truecount += 1 NEW_LINE isRunning [ ele ] = True NEW_LINE DEDENT else : NEW_LINE INDENT mini = 100000 NEW_LINE miniind = 0 NEW_LINE for j in range ( 1 , m + 1 ) : NEW_LINE INDENT if isRunning [ j ] and mini > freqarr [ i ] [ j ] : NEW_LINE INDENT mini = freqarr [ i ] [ j ] NEW_LINE miniind = j NEW_LINE DEDENT DEDENT if mini == 0 : NEW_LINE INDENT isRunning [ miniind ] = False NEW_LINE isRunning [ ele ] = True NEW_LINE cost += 1 NEW_LINE DEDENT else : NEW_LINE INDENT farpos = find ( arr , i , m , isRunning ) NEW_LINE isRunning [ arr [ farpos ] ] = False NEW_LINE isRunning [ ele ] = True NEW_LINE cost += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return cost NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n1 , m1 = 3 , 6 NEW_LINE arr1 = [ 1 , 2 , 1 , 3 , 4 , 1 ] NEW_LINE print ( mincost ( n1 , m1 , arr1 ) ) NEW_LINE n2 , m2 = 2 , 6 NEW_LINE arr2 = [ 1 , 2 , 1 , 3 , 2 , 1 ] NEW_LINE print ( mincost ( n2 , m2 , arr2 ) ) NEW_LINE n3 , m3 = 3 , 31 NEW_LINE arr3 = [ 7 , 11 , 17 , 10 , 7 , 10 , 2 , 9 , 2 , 18 , 8 , 10 , 20 , 10 , 3 , 20 , 17 , 17 , 17 , 1 , 15 , 10 , 8 , 3 , 3 , 18 , 13 , 2 , 10 , 10 , 11 ] NEW_LINE print ( mincost ( n3 , m3 , arr3 ) ) NEW_LINE DEDENT