Minimum time required to complete all tasks without altering their order
Last Updated :
27 Dec, 2023
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++
#include <bits/stdc++.h>
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;
}
|
Java
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);
}
}
|
Python3
def findMinimumTime(tasks, K):
last_time_map = {}
curr_time = 0
for c in tasks:
if c in last_time_map:
if curr_time - last_time_map < = K:
curr_time + = K - (curr_time - last_time_map) + 1
last_time_map = curr_time
curr_time + = 1
print (curr_time)
if __name__ = = "__main__" :
S = "ABACCA"
K = 2
findMinimumTime(S, K)
|
C#
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);
}
}
|
Javascript
<script>
function findMinimumTime(tasks, K)
{
var map = new Map();
var curr_time = 0;
tasks.split( '' ).forEach(c => {
if (map.has(c)) {
if (curr_time - map.get(c) <= K) {
curr_time += K - (curr_time - map.get(c)) + 1;
}
}
map.set(c, curr_time);
curr_time++;
});
document.write( curr_time);
}
var S = "ABACCA" ;
var K = 2;
findMinimumTime(S, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...