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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMinimumTime ( string & S , int N , int K ) { if ( N == 0 ) { cout << 0 ; return ; } int maxfreq = INT_MIN ; char maxchar ; unordered_map < char , int > um ; for ( int i = 0 ; i < N ; i ++ ) { um [ S [ i ] ] ++ ; if ( um [ S [ i ] ] > maxfreq ) { maxfreq = um [ S [ i ] ] ; maxchar = S [ i ] ; } } int emptySlots = ( maxfreq - 1 ) * K ; for ( auto & it : um ) { if ( it . first == maxchar ) continue ; emptySlots -= min ( it . second , maxfreq - 1 ) ; } int ans = N + max ( 0 , emptySlots ) ; cout << ans ; } int main ( ) { string S = " AAABBB " ; int K = 2 ; int N = S . length ( ) ; findMinimumTime ( S , N , K ) ; return 0 ; }