Minimum time required to complete all tasks with alteration of their order allowed | Python3 program for the above approach ; Function to find the minimum time required to complete the tasks if the order of tasks can be changed ; If there is no task , print 0 ; Store the maximum occurring character and its frequency ; Stores the frequency of each character ; Traverse the string S ; Increment the frequency of the current character ; Update maxfreq and maxchar ; Store the number of empty slots ; Traverse the hashmap , um ; Fill the empty slots ; Store the required result ; Print the result ; Driver Code
import sys NEW_LINE from collections import defaultdict NEW_LINE def findMinimumTime ( S , N , K ) : NEW_LINE INDENT if ( N == 0 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE return NEW_LINE DEDENT maxfreq = - sys . maxsize NEW_LINE um = defaultdict ( int ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT um [ S [ i ] ] += 1 NEW_LINE if ( um [ S [ i ] ] > maxfreq ) : NEW_LINE INDENT maxfreq = um [ S [ i ] ] NEW_LINE maxchar = S [ i ] NEW_LINE DEDENT DEDENT emptySlots = ( maxfreq - 1 ) * K NEW_LINE for it in um : NEW_LINE INDENT if ( it == maxchar ) : NEW_LINE INDENT continue NEW_LINE DEDENT emptySlots -= min ( um [ it ] , maxfreq - 1 ) NEW_LINE DEDENT ans = N + max ( 0 , emptySlots ) NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " AAABBB " NEW_LINE K = 2 NEW_LINE N = len ( S ) NEW_LINE findMinimumTime ( S , N , K ) NEW_LINE DEDENT