Minimum time required to complete all tasks with alteration of their order allowed
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 any order, given 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 = “AAABBB”, K = 2
Output: 8
Explanation:
Below are the order of task that is to be completed:
A ? B ? _ ? A ? B ? _ ? A ? B
Therefore, the total time required is 8 units.
Input: S = “AAABBB”, K = 0
Output: 6
Approach: The given problem can be solved based on the following observations:
- To complete the tasks in minimum time, the idea is to find the maximum occurring character in the array and complete those tasks first.
- Let the maximum occurring character be X and its frequency be freq.
- Now, to complete tasks of type X first, there must be difference of K between them, which leaves a total number of (freq – 1) * K empty slots in between the tasks of X.
- Then, fill these empty slots by traversing the other tasks since the other tasks have frequency less than or equal to that of X.
Follow the steps below to solve the problem:
- Initialize a HashMap, say M, that stores the frequency of each character in the given string S.
- Initialize two variables, maxchar and maxfreq, that stores the maximum occurring character and its frequency respectively.
- Traverse the given string S and increment the frequency of S[i] in the map M and if it is greater than maxfreq, and update the value of maxchar to S[i] and the value of maxfreq to its frequency.
- Store the number of empty slots in a variable, say S, as (maxfreq – 1) * K.
- Traverse the HashMap, M and for every character other than maxchar, update the value of empty slots, S by subtracting minimum of (maxfreq – 1) and M[S[i]] from S.
- Store the required minimum time in a variable, ans as sum of N and S, if the value of S ? 0.
- 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& S, int N,
int K)
{
if (N == 0) {
cout << 0;
return ;
}
int maxfreq = INT_MIN;
char maxchar;
unordered_map< char , int > um;
for ( int i = 0; i < N; i++) {
um[S[i]]++;
if (um[S[i]] > maxfreq) {
maxfreq = um[S[i]];
maxchar = S[i];
}
}
int emptySlots = (maxfreq - 1) * K;
for ( auto & it : um) {
if (it.first == maxchar)
continue ;
emptySlots -= min(it.second,
maxfreq - 1);
}
int ans = N + max(0, emptySlots);
cout << ans;
}
int main()
{
string S = "AAABBB" ;
int K = 2;
int N = S.length();
findMinimumTime(S, N, K);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
class GFG{
static void findMinimumTime(String S, int N, int K)
{
if (N == 0 )
{
System.out.println( 0 );
return ;
}
int maxfreq = Integer.MIN_VALUE;
char maxchar = ' ' ;
HashMap<Character, Integer> um = new HashMap<>();
for ( int i = 0 ; i < N; i++)
{
um.put(S.charAt(i),
um.getOrDefault(S.charAt(i), 0 ) + 1 );
if (um.get(S.charAt(i)) > maxfreq)
{
maxfreq = um.get(S.charAt(i));
maxchar = S.charAt(i);
}
}
int emptySlots = (maxfreq - 1 ) * K;
for (Map.Entry<Character, Integer> it :
um.entrySet())
{
if (it.getKey() == maxchar)
continue ;
emptySlots -= Math.min(
it.getValue(), maxfreq - 1 );
}
int ans = N + Math.max( 0 , emptySlots);
System.out.println(ans);
}
public static void main(String[] args)
{
String S = "AAABBB" ;
int K = 2 ;
int N = S.length();
findMinimumTime(S, N, K);
}
}
|
Python3
import sys
from collections import defaultdict
def findMinimumTime(S, N, K):
if (N = = 0 ):
print ( 0 )
return
maxfreq = - sys.maxsize
um = defaultdict( int )
for i in range (N):
um[S[i]] + = 1
if (um[S[i]] > maxfreq):
maxfreq = um[S[i]]
maxchar = S[i]
emptySlots = (maxfreq - 1 ) * K
for it in um:
if (it = = maxchar):
continue
emptySlots - = min (um[it],
maxfreq - 1 )
ans = N + max ( 0 , emptySlots)
print (ans)
if __name__ = = "__main__" :
S = "AAABBB"
K = 2
N = len (S)
findMinimumTime(S, N, K)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void findMinimumTime( string S, int N, int K)
{
if (N == 0)
{
Console.Write(0);
return ;
}
int maxfreq = Int32.MinValue;
char maxchar = ' ' ;
Dictionary< char ,
int > um = new Dictionary< char ,
int >();
for ( int i = 0; i < N; i++)
{
if (um.ContainsKey(S[i]))
um[S[i]]++;
else
um.Add(S[i], 1);
if (um.ContainsKey(S[i]) &&
um[S[i]] > maxfreq)
{
maxfreq = um[S[i]];
maxchar = S[i];
}
}
int emptySlots = (maxfreq - 1) * K;
foreach (KeyValuePair< char , int > kvp in um)
{
if (kvp.Key == maxchar)
continue ;
emptySlots -= Math.Min(kvp.Value,
maxfreq - 1);
}
int ans = N + Math.Max(0, emptySlots);
Console.Write(ans);
}
public static void Main()
{
string S = "AAABBB" ;
int K = 2;
int N = S.Length;
findMinimumTime(S, N, K);
}
}
|
Javascript
<script>
function findMinimumTime(s, N, K)
{
if (N == 0)
{
document.write(0);
return ;
}
let maxfreq = Number.MIN_SAFE_INTEGER;
let maxchar;
let um = new Map();
for (let i = 0; i < N; i++)
{
if (um.has(s[i]))
{
um.set(s[i], um.get(s[i]) + 1)
}
else
{
um.set(s[i], 1)
}
if (um.get(S[i]) > maxfreq)
{
maxfreq = um.get(S[i]);
maxchar = S[i];
}
}
let emptySlots = (maxfreq - 1) * K;
for (let it of um)
{
if (it[0] == maxchar)
continue ;
emptySlots -= Math.min(
it[1], maxfreq - 1);
}
let ans = N + Math.max(0, emptySlots);
document.write(ans);
}
let S = "AAABBB" ;
let K = 2;
let N = S.length;
findMinimumTime(S, N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(256)
Last Updated :
15 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...