📌  相关文章
📜  完成所有任务所需的最短时间,允许更改订单

📅  最后修改于: 2021-09-04 15:03:13             🧑  作者: Mango

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

例子:

方法:根据以下观察可以解决给定的问题:

  • 为了在最短的时间内完成任务,想法是找到数组中出现次数最多的字符并首先完成这些任务。
  • 假设出现的最大字符为X ,其频率为freq
  • 现在,X型的完成任务第一,必须有被K之间,留下的总数(频率- 1)的差异在X的任务之间* K空插槽。
  • 然后,通过遍历其他任务来填充这些空槽,因为其他任务的频率小于或等于X 的频率。

请按照以下步骤解决问题:

  • 初始化一个 HashMap,比如M ,它存储给定字符串S中每个字符的频率。
  • 初始化两个变量, maxcharmaxfreq ,分别存储最大出现的字符及其频率。
  • 遍历给定的字符串S并增加S[i]在映射M 中的频率,如果它大于maxfreq 并将maxchar的值更新为S[i] ,将maxfreq的值更新为其频率。
  • 将空槽的数量存储在一个变量中,比如S ,如(maxfreq – 1) * K
  • M [S [I]]选自S 遍历HashMap中,M和比maxchar其他每一个字符,更新空时隙的值,通过减去(1 maxfreq)的最小S上
  • 存储所需的最小时间的变量,ANS作为NS之和,若S≥0的值。
  • 完成以上步骤后,打印ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum time
// required to complete the tasks if
// the order of tasks can be changed
void findMinimumTime(string& S, int N,
                     int K)
{
    // If there is no task, print 0
    if (N == 0) {
        cout << 0;
        return;
    }
 
    // Store the maximum occurring
    // character and its frequency
    int maxfreq = INT_MIN;
    char maxchar;
 
    // Stores the frequency of each
    // character
    unordered_map um;
 
    // Traverse the string S
    for (int i = 0; i < N; i++) {
 
        // Increment the frequency of
        // the current character
        um[S[i]]++;
 
        // Update maxfreq and maxchar
        if (um[S[i]] > maxfreq) {
            maxfreq = um[S[i]];
            maxchar = S[i];
        }
    }
 
    // Store the number of empty slots
    int emptySlots = (maxfreq - 1) * K;
 
    // Traverse the hashmap, um
    for (auto& it : um) {
 
        if (it.first == maxchar)
            continue;
 
        // Fill the empty slots
        emptySlots -= min(it.second,
                          maxfreq - 1);
    }
 
    // Store the required result
    int ans = N + max(0, emptySlots);
 
    // Print the result
    cout << ans;
}
 
// Driver Code
int main()
{
    string S = "AAABBB";
    int K = 2;
    int N = S.length();
    findMinimumTime(S, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.HashMap;
import java.util.Map;
 
class GFG{
 
// Function to find the minimum time
// required to complete the tasks if
// the order of tasks can be changed
static void findMinimumTime(String S, int N, int K)
{
     
    // If there is no task, print 0
    if (N == 0)
    {
        System.out.println(0);
        return;
    }
 
    // Store the maximum occurring
    // character and its frequency
    int maxfreq = Integer.MIN_VALUE;
    char maxchar = ' ';
 
    // Stores the frequency of each
    // character
    HashMap um = new HashMap<>();
 
    // Traverse the string S
    for(int i = 0; i < N; i++)
    {
         
        // Increment the frequency of
        // the current character
        um.put(S.charAt(i),
               um.getOrDefault(S.charAt(i), 0) + 1);
 
        // Update maxfreq and maxchar
        if (um.get(S.charAt(i)) > maxfreq)
        {
            maxfreq = um.get(S.charAt(i));
            maxchar = S.charAt(i);
        }
    }
 
    // Store the number of empty slots
    int emptySlots = (maxfreq - 1) * K;
 
    // Traverse the hashmap, um
    for(Map.Entry it :
        um.entrySet())
    {
        if (it.getKey() == maxchar)
            continue;
 
        // Fill the empty slots
        emptySlots -= Math.min(
            it.getValue(), maxfreq - 1);
    }
 
    // Store the required result
    int ans = N + Math.max(0, emptySlots);
 
    // Print the result
    System.out.println(ans);
}
 
// Driver code
public static void main(String[] args)
{
    String S = "AAABBB";
    int K = 2;
    int N = S.length();
     
    findMinimumTime(S, N, K);
}
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
import sys
from collections import defaultdict
 
# Function to find the minimum time
# required to complete the tasks if
# the order of tasks can be changed
def findMinimumTime(S, N, K):
     
    # If there is no task, print 0
    if (N == 0):
        print(0)
        return
 
    # Store the maximum occurring
    # character and its frequency
    maxfreq = -sys.maxsize
 
    # Stores the frequency of each
    # character
    um = defaultdict(int)
 
    # Traverse the string S
    for i in range(N):
 
        # Increment the frequency of
        # the current character
        um[S[i]] += 1
 
        # Update maxfreq and maxchar
        if (um[S[i]] > maxfreq):
            maxfreq = um[S[i]]
            maxchar = S[i]
 
    # Store the number of empty slots
    emptySlots = (maxfreq - 1) * K
 
    # Traverse the hashmap, um
    for it in um:
        if (it == maxchar):
            continue
 
        # Fill the empty slots
        emptySlots -= min(um[it],
                          maxfreq - 1)
 
    # Store the required result
    ans = N + max(0, emptySlots)
 
    # Print the result
    print(ans)
 
# Driver Code
if __name__ == "__main__":
 
    S = "AAABBB"
    K = 2
    N = len(S)
     
    findMinimumTime(S, N, K)
     
# This code is contributed by ukasp


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the minimum time
// required to complete the tasks if
// the order of tasks can be changed
static void findMinimumTime(string S, int N, int K)
{
     
    // If there is no task, print 0
    if (N == 0)
    {
        Console.Write(0);
        return;
    }
 
    // Store the maximum occurring
    // character and its frequency
    int maxfreq = Int32.MinValue;
    char maxchar = ' ';
 
    // Stores the frequency of each
    // character
    Dictionary um = new Dictionary();
 
    // Traverse the string S
    for(int i = 0; i < N; i++)
    {
         
        // Increment the frequency of
        // the current character
        if (um.ContainsKey(S[i]))
            um[S[i]]++;
        else
            um.Add(S[i], 1);
 
        // Update maxfreq and maxchar
        if (um.ContainsKey(S[i]) &&
            um[S[i]] > maxfreq)
        {
            maxfreq = um[S[i]];
            maxchar = S[i];
        }
    }
 
    // Store the number of empty slots
    int emptySlots = (maxfreq - 1) * K;
 
    // Traverse the hashmap, um
    foreach(KeyValuePair kvp in um)
    {
 
        if (kvp.Key == maxchar)
            continue;
 
        // Fill the empty slots
        emptySlots -= Math.Min(kvp.Value,
                               maxfreq - 1);
    }
 
    // Store the required result
    int ans = N + Math.Max(0, emptySlots);
 
    // Print the result
    Console.Write(ans);
}
 
// Driver Code
public static void Main()
{
    string S = "AAABBB";
    int K = 2;
    int N = S.Length;
     
    findMinimumTime(S, N, K);
}
}
 
// This code is contributed by bgangwar59


输出:
8

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

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