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
using System ; using System . Collections . Generic ; class GFG { static void findMinimumTime ( String tasks , int K ) { Dictionary < char , int > map = new Dictionary < char , int > ( ) ; int curr_time = 0 ; foreach ( char c in tasks . ToCharArray ( ) ) { if ( map . ContainsKey ( c ) ) { if ( curr_time - map <= K ) { curr_time += K - ( curr_time - map ) + 1 ; } } if ( ! map . ContainsKey ( c ) ) map . Add ( c , curr_time ) ; else map = curr_time ; curr_time ++ ; } Console . WriteLine ( curr_time ) ; } public static void Main ( String [ ] args ) { String S = " ABACCA " ; int K = 2 ; findMinimumTime ( S , K ) ; } }