Minimum time required to complete all tasks without altering their order | 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
def findMinimumTime ( tasks , K ) : NEW_LINE INDENT map = { } NEW_LINE curr_time = 0 NEW_LINE for c in tasks : NEW_LINE INDENT if ( c in map ) : NEW_LINE INDENT if ( curr_time - map <= K ) : NEW_LINE INDENT curr_time += K - ( curr_time - map ) + 1 NEW_LINE DEDENT DEDENT map = curr_time NEW_LINE curr_time += 1 NEW_LINE DEDENT print ( curr_time ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " ABACCA " NEW_LINE K = 2 NEW_LINE findMinimumTime ( S , K ) NEW_LINE DEDENT