Given a string S consisting of N characters (representing the tasks to perform) and a positive integer K, the task is to find the minimum time required to complete all the given tasks in the given order such that each task takes one unit of time and each task of the same type must be performed at an interval of K units.
Examples:
Input: S = “ABACCA”, K = 2
Output: 9
Explanation:
Below are the order of task that is to be completed:
A ? B ? _ ? A ? C ? _ ? _ ? C ? A.
Therefore, the total time required is 9 units.Input: S = “AAAA”, K = 3
Output: 13
Approach: The given problem can be solved by Hashing by keeping track of the last instant of each task and find the overall minimum time accordingly. Follow the steps below to solve the problem:
- Initialize a hashmap, say M, to keep track of the last time instant of each task.
- Initialize a variable, say ans as 0, to store the resultant minimum time.
-
Traverse the given string S and perform the following steps:
- If the character S[i] is present in M, then store the last instant of S[i] in a variable, say t.
- If the value of (ans – t) is at most K, then add the value of K – (ans – t) + 1 to variable ans.
- Update the last time instant of the character S[i] in M to ans, and increment the value of ans by 1.
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
// C++ implementation of // the above approach #include <bits/stdc++.h> using namespace std;
// Function to find the minimum // time required to complete // tasks without changing their order void findMinimumTime(string tasks, int K)
{ // Keeps track of the last
// time instant of each task
unordered_map< char , int > map;
// Stores the required result
int curr_time = 0;
// Traverse the given string
for ( char c : tasks) {
// Check last time instant of
// task, if it exists before
if (map.find(c) != map.end()) {
// Increment the time
// if task is within
// the K units of time
if (curr_time - map <= K) {
curr_time += K - (curr_time - map) + 1;
}
}
// Update the time of the
// current task in the map
map = curr_time;
// Increment the time by 1
curr_time++;
}
// Print the result
cout << curr_time;
} // Driver Code int main()
{ string S = "ABACCA" ;
int K = 2;
findMinimumTime(S, K);
return 0;
} // This code is contributed by Kingash. |
// Java program for the above approach import java.util.*;
class GFG {
// Function to find the minimum
// time required to complete
// tasks without changing their order
static void findMinimumTime(
String tasks, int K)
{
// Keeps track of the last
// time instant of each task
Map<Character, Integer> map
= new HashMap<>();
// Stores the required result
int curr_time = 0 ;
// Traverse the given string
for ( char c : tasks.toCharArray()) {
// Check last time instant of
// task, if it exists before
if (map.containsKey(c)) {
// Increment the time
// if task is within
// the K units of time
if (curr_time
- map.get(c)
<= K) {
curr_time += K
- (curr_time
- map.get(c))
+ 1 ;
}
}
// Update the time of the
// current task in the map
map.put(c, curr_time);
// Increment the time by 1
curr_time++;
}
// Print the result
System.out.println(curr_time);
}
// Driver Code
public static void main(String[] args)
{
String S = "ABACCA" ;
int K = 2 ;
findMinimumTime(S, K);
}
} |
# Python 3 implementation of # the above approach # Function to find the minimum # time required to complete # tasks without changing their order def findMinimumTime(tasks, K):
# Keeps track of the last
# time instant of each task
last_time_map = {}
# Stores the required result
curr_time = 0
# Traverse the given string
for c in tasks:
# Check last time instant of
# task, if it exists before
if c in last_time_map:
# Increment the time
# if task is within
# the K units of time
if curr_time - last_time_map < = K:
curr_time + = K - (curr_time - last_time_map) + 1
# Update the time of the
# current task in the map
last_time_map = curr_time
# Increment the time by 1
curr_time + = 1
# Print the result
print (curr_time)
# Driver Code if __name__ = = "__main__" :
S = "ABACCA"
K = 2
findMinimumTime(S, K)
|
// C# implementation of // the above approach using System;
using System.Collections.Generic;
class GFG{
// Function to find the minimum // time required to complete // tasks without changing their order static void findMinimumTime(String tasks, int K)
{ // Keeps track of the last
// time instant of each task
Dictionary< char ,
int > map = new Dictionary< char ,
int >();
// Stores the required result
int curr_time = 0;
// Traverse the given string
foreach ( char c in tasks.ToCharArray())
{
// Check last time instant of
// task, if it exists before
if (map.ContainsKey(c))
{
// Increment the time
// if task is within
// the K units of time
if (curr_time - map <= K)
{
curr_time += K - (curr_time - map) + 1;
}
}
// Update the time of the
// current task in the map
if (!map.ContainsKey(c))
map.Add(c, curr_time);
else
map = curr_time;
// Increment the time by 1
curr_time++;
}
// Print the result
Console.WriteLine(curr_time);
} // Driver Code public static void Main(String[] args)
{ String S = "ABACCA" ;
int K = 2;
findMinimumTime(S, K);
} } // This code is contributed by shikhasingrajput |
<script> // Javascript implementation of // the above approach // Function to find the minimum // time required to complete // tasks without changing their order function findMinimumTime(tasks, K)
{ // Keeps track of the last
// time instant of each task
var map = new Map();
// Stores the required result
var curr_time = 0;
// Traverse the given string
tasks.split( '' ).forEach(c => {
// Check last time instant of
// task, if it exists before
if (map.has(c)) {
// Increment the time
// if task is within
// the K units of time
if (curr_time - map.get(c) <= K) {
curr_time += K - (curr_time - map.get(c)) + 1;
}
}
// Update the time of the
// current task in the map
map.set(c, curr_time);
// Increment the time by 1
curr_time++;
});
// Print the result
document.write( curr_time);
} // Driver Code var S = "ABACCA" ;
var K = 2;
findMinimumTime(S, K); // This code is contributed by rutvik_56. </script> |
9
Time Complexity: O(N)
Auxiliary Space: O(1)