📌  相关文章
📜  在不改变任务顺序的情况下完成所有任务所需的最短时间

📅  最后修改于: 2021-09-06 05:41:37             🧑  作者: Mango

给定一个由N 个字符(代表要执行的任务)和一个正整数K组成的字符串S ,任务是找到按给定顺序完成所有给定任务所需的最短时间,使得每个任务需要一个单位时间和每个相同类型的任务必须以K 个单位的间隔执行。

例子:

方法:通过跟踪每个任务的最后时刻并相应地找到总体最短时间,可以通过哈希解决给定问题。请按照以下步骤解决问题:

  • 初始化一个哈希图,比如M ,以跟踪每个任务的最后时刻。
  • 初始化一个变量,比如ans0 ,以存储结果的最小时间。
  • 遍历给定的字符串S并执行以下步骤:
    • 如果字符S[i]存在于M 中,则将S[i]的最后时刻存储在一个变量中,比如t
    • 如果(ans – t)的值至多为 K ,则 K – (ans – t) + 1的值添加到变量ans
    • 更新字符S [i]MANS的最后时刻,以及递增1 ANS的值。
  • 完成以上步骤后,打印ans的值作为结果。

下面是上述方法的实现:

C++
// C++ implementation of
// the above approach
#include 
 
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 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
// 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 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);
    }
}


Python3
# 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
    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 map):
 
            # 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 += 1
 
    # Print the result
    print(curr_time)
 
 
# Driver Code
if __name__ == "__main__":
 
    S = "ABACCA"
    K = 2
    findMinimumTime(S, K)
 
  #  This code is contributed by ukasp.


C#
// 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 map = new Dictionary();
 
    // 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


Javascript


输出:
9

时间复杂度: O(N)
辅助空间: O(256)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live