Minimum time required to complete all tasks with alteration of their order allowed | 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
function findMinimumTime ( s , N , K ) { if ( N == 0 ) { document . write ( 0 ) ; return ; } let maxfreq = Number . MIN_SAFE_INTEGER ; let maxchar ; let um = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( um . has ( s [ i ] ) ) { um . set ( s [ i ] , um . get ( s [ i ] ) + 1 ) } else { um . set ( s [ i ] , 1 ) } if ( um . get ( S [ i ] ) > maxfreq ) { maxfreq = um . get ( S [ i ] ) ; maxchar = S [ i ] ; } } let emptySlots = ( maxfreq - 1 ) * K ; for ( let it of um ) { if ( it [ 0 ] == maxchar ) continue ; emptySlots -= Math . min ( it [ 1 ] , maxfreq - 1 ) ; } let ans = N + Math . max ( 0 , emptySlots ) ; document . write ( ans ) ; } let S = " " ; let K = 2 ; let N = S . length ; findMinimumTime ( S , N , K ) ;