Minimum time required to complete all tasks without altering their order


In this problem, we need to find the total time required to complete all tasks according to the given condition.

We can use the map data structure to solve the problem. We can keep tracking the last performed time for each task, and if the time interval is less than K, we can increment time units accordingly.

Problem statement – We have given a string task containing alphabetical characters of length N. Each character represents a task, and we need one unit of time to perform the tasks. Also, the condition is that each task should be performed at the interval of K units. Here, K is the positive integer that is given in the input. Print the total time units required to complete all tasks in the given order.

Sample examples

Input– str = "BADBBC", K = 3

Output– 10

Explanation– We can perform the task in the given order.

B -> A -> D -> _ -> B -> _ -> _ -> _ -> B -> C

Here, programmers can observe that ‘_’ represents the single time unit to skip to perform the same tasks in the interval of 3.

Input– str = “RSTUV”, K = 2

Output– 5

ExplanationWe can perform the task in the given order.

R -> S -> T -> U -> V

Input– str = ‘HHHH’, K = 2

Output– 10

Explanation– We can perform the task in the given order.

H -> _ -> _ -> H -> _ -> _ -> H -> _ -> _ -> H

Approach 1

In this approach, we will store the last performed time of each task in the hash map. If any task is repeated, and the time interval between the current time and when the task was performed last time is less than K, we need to add extra time units to make the time interval equal to K.

Algorithm

  • Define the unordered map named ‘time’ to track the time unit when the particular task is performed for the last time.

  • Initialize the ‘ans’ with zero to store the total time unit.

  • Traverse the task string.

  • If the current character exists in the map, it means the same task is already performed.

    • Find the time unit from the map when the current task is performed for the last time. If the difference between the ‘ans’ (current time unit) and time[ch] is less than K, we need to add some time units to the ans by following the step 4.2 given below.

    • Subtract the ans - time[ch] from the K and add 1 to K. After that, add the resultant value into the ans.

  • Store the value of ‘ans’ in the map for the ‘ch’ character.

  • Increase the value of ‘ans’ by 1.

  • Return the value of ‘ans’.

Example

#include <bits/stdc++.h>
using namespace std;
// function to find the minimum time required to complete the tasks according to the given conditions
int minimumTime(string tasks, int K){
    // Keeps track of the last time instant of each task
    unordered_map<char, int> time;
    // to keep track of the current time
    int ans = 0;
    // Traverse the given string
    for (char ch : tasks){
        // Check if the same task exists in the map
        if (time.find(ch) != time.end()){
            // If the same task exists and its last time instant is less than K, then update the time of the current task.
            if (ans - time[ch] <= K){
                ans += K - (ans - time[ch]) + 1;
            }
        }
        // Update the last time instant of the current task
        time[ch] = ans;
        // Increase the current time by 1
        ans++;
    }
    // return the minimum time required
    return ans;
}

int main(){
    string str = "BADBBC";
    int K = 3;
    cout << "The minimum time required to complete all tasks is " << minimumTime(str, K);
    return 0;
}

Output

The minimum time required to complete all tasks is 10

Time complexity – O(N), as we iterate the string.

Space complexity – O(1), as we don’t use any dynamic space.

Updated on: 18-Aug-2023

77 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements