📜  达到给定分数所需的最短时间

📅  最后修改于: 2021-05-17 05:51:20             🧑  作者: Mango

给定一个整数目标和一个由N个正整数组成的数组arr [] ,其中arr [i]表示为第i数组元素得分1分所需的时间,任务是找到从中获得得分目标所需的最短时间给定的数组。

例子:

方法:想法是使用哈希存储数组元素的频率,并在Hashmap上进行迭代,并不断更新分数,直到达到目标为止。一个重要的观察结果是,任何元素arr [i]在任意时刻t都将t / arr [i]添加到分数中。请按照以下步骤解决问题:

  • 初始化一个变量,表示的时间,以存储到存储在任何时刻t的得分需要和总和0的最小时间。
  • 将数组元素的频率存储在Hashmap M中
  • 迭代直到总和小于目标并执行以下步骤:
    • sum设置为0并将time的值增加1
    • 现在,遍历哈希图M,并在每次迭代中以频率*(时间/值)递增sum
  • 完成上述步骤后,打印时间值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate minimum time
// required to achieve given score target
void findMinimumTime(int* p, int n,
                     int target)
{
    // Store the frequency of elements
    unordered_map um;
 
    // Traverse the array p[]
    for (int i = 0; i < n; i++) {
 
        // Update the frequency
        um[p[i]]++;
    }
 
    // Stores the minimim time required
    int time = 0;
 
    // Store the current score
    // at any time instant t
    int sum = 0;
 
    // Iterate until sum is at
    // least equal to target
    while (sum < target) {
 
        sum = 0;
 
        // Increment time with
        // every iteration
        time++;
 
        // Traverse the map
        for (auto& it : um) {
 
            // Increment the points
            sum += it.second
                   * (time / it.first);
        }
    }
 
    // Print the time required
    cout << time;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 3, 3, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int target = 10;
 
    // Function Call
    findMinimumTime(arr, N, target);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
import java.io.*;
  
class GFG{
      
// Function to calculate minimum time
// required to achieve given score target
static void findMinimumTime(int [] p, int n,
                            int target)
{
      
    // Store the frequency of elements
    HashMap um = new HashMap<>();
                                          
    // Traverse the array p[]
    for(int i = 0; i < n; i++)
    {
          
        // Update the frequency
        if (!um.containsKey(p[i]))
            um.put(p[i], 1);
        else
            um.put(p[i],
            um.get(p[i]) + 1);
    }
  
    // Stores the minimim time required
    int time = 0;
  
    // Store the current score
    // at any time instant t
    int sum = 0;
      
    while (sum < target)
    {
        sum = 0;
          
        // Increment time with
        // every iteration
        time++;
  
        // Traverse the map
        for(Map.Entry it : um.entrySet())
        {
              
            // Increment the points
            sum += it.getValue() * (time / it.getKey());
        }
    }
  
    // Print the time required
    System.out.println(time);
}
 
// Driver Code
public static void main(String args[])
{
    int[] arr = { 1, 3, 3, 4 };
    int N = arr.length;
    int target = 10;
      
    // Function Call
    findMinimumTime(arr, N, target);
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program for the above approach
 
# Function to calculate minimum time
# required to achieve given score target
def findMinimumTime(p, n, target):
     
    # Store the frequency of elements
    um = {}
 
    # Traverse the array p[]
    for i in range(n):
 
        # Update the frequency
        um[p[i]] = um.get(p[i], 0) + 1
 
    # Stores the minimim time required
    time = 0
 
    # Store the current score
    # at any time instant t
    sum = 0
 
    # Iterate until sum is at
    # least equal to target
    while (sum < target):
 
        sum = 0
 
        # Increment time with
        # every iteration
        time += 1
 
        # Traverse the map
        for it in um:
 
            # Increment the points
            sum += um[it] * (time // it)
 
    # Prthe time required
    print(time)
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 3, 3, 4]
    N = len(arr)
    target = 10
 
    # Function Call
    findMinimumTime(arr, N, target)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System.Collections.Generic;
using System;
using System.Linq;
 
class GFG{
 
// Function to calculate minimum time
// required to achieve given score target
static void findMinimumTime(int [] p, int n,
                            int target)
{
     
    // Store the frequency of elements
    Dictionary um = new Dictionary();
                                         
    // Traverse the array p[]
    for(int i = 0; i < n; i++)
    {
         
        // Update the frequency
        if (um.ContainsKey(p[i]) == true)
            um[p[i]] += 1;
        else
            um[p[i]] = 1;
    }
 
    // Stores the minimim time required
    int time = 0;
 
    // Store the current score
    // at any time instant t
    int sum = 0;
 
    // Iterate until sum is at
    // least equal to target
    var val = um.Keys.ToList();
     
    while (sum < target)
    {
        sum = 0;
         
        // Increment time with
        // every iteration
        time++;
 
        // Traverse the map
        foreach(var key in val)
        {
             
            // Increment the points
            sum += um[key] * (time / key);
        }
    }
 
    // Print the time required
    Console.WriteLine(time);
}
 
// Driver Code
public static void Main()
{
    int []arr = { 1, 3, 3, 4 };
    int N = arr.Length;
    int target = 10;
     
    // Function Call
    findMinimumTime(arr, N, target);
}
}
 
// This code is contributed by Stream_Cipher


输出
6

时间复杂度: O(目标* N)
辅助空间: O(N)