Minimum time required to complete all tasks with alteration of their order allowed | C # 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
using System ; using System . Collections . Generic ; class GFG { static void findMinimumTime ( string S , int N , int K ) { if ( N == 0 ) { Console . Write ( 0 ) ; return ; } int maxfreq = Int32 . MinValue ; char maxchar = ' ▁ ' ; Dictionary < char , int > um = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( um . ContainsKey ( S [ i ] ) ) um [ S [ i ] ] ++ ; else um . Add ( S [ i ] , 1 ) ; if ( um . ContainsKey ( S [ i ] ) && um [ S [ i ] ] > maxfreq ) { maxfreq = um [ S [ i ] ] ; maxchar = S [ i ] ; } } int emptySlots = ( maxfreq - 1 ) * K ; foreach ( KeyValuePair < char , int > kvp in um ) { if ( kvp . Key == maxchar ) continue ; emptySlots -= Math . Min ( kvp . Value , maxfreq - 1 ) ; } int ans = N + Math . Max ( 0 , emptySlots ) ; Console . Write ( ans ) ; } public static void Main ( ) { string S = " AAABBB " ; int K = 2 ; int N = S . Length ; findMinimumTime ( S , N , K ) ; } }