Minimum time required to complete all tasks without altering their order | C ++ implementation of the above approach ; Function to find the minimum time required to complete tasks without changing their order ; Keeps track of the last time instant of each task ; Stores the required result ; Traverse the given string ; Check last time instant of task , if it exists before ; Increment the time if task is within the K units of time ; Update the time of the current task in the map ; Increment the time by 1 ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMinimumTime ( string tasks , int K ) { unordered_map < char , int > map ; int curr_time = 0 ; for ( char c : tasks ) { if ( map . find ( c ) != map . end ( ) ) { if ( curr_time - map <= K ) { curr_time += K - ( curr_time - map ) + 1 ; } } map = curr_time ; curr_time ++ ; } cout << curr_time ; } int main ( ) { string S = " ABACCA " ; int K = 2 ; findMinimumTime ( S , K ) ; return 0 ; }