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

📅  最后修改于: 2021-10-26 05:48:59             🧑  作者: Mango

给定的整数对象并在需要改编[i]表示的时间记1i数组元素的数组ARR []N个正整数,任务是找到最小时间需要获得来自得分目标给定的数组。

例子:

方法:思路是使用Hashing来存储数组元素的频率,迭代Hashmap,不断更新分数,直到达到target 。一个重要的观察结果是,任何元素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


Javascript


输出
6

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程