Minimum time required to complete all tasks without altering their order | Java program for 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
import java . util . * ; class GFG { static void findMinimumTime ( String tasks , int K ) { Map < Character , Integer > map = new HashMap < > ( ) ; int curr_time = 0 ; for ( char c : tasks . toCharArray ( ) ) { if ( map . containsKey ( c ) ) { if ( curr_time - map . get ( c ) <= K ) { curr_time += K - ( curr_time - map . get ( c ) ) + 1 ; } } map . put ( c , curr_time ) ; curr_time ++ ; } System . out . println ( curr_time ) ; } public static void main ( String [ ] args ) { String S = " ABACCA " ; int K = 2 ; findMinimumTime ( S , K ) ; } }