给定的整数对象并在需要改编[i]表示的时间记1分的第i个数组元素的数组ARR []由N个正整数,任务是找到最小时间需要获得来自得分目标给定的数组。
例子:
Input: arr[] = {1, 3, 3, 4}, target = 10
Output: 6
Explanation:
At time instant, t = 1: Score of the array: {1, 0, 0, 0}
At time instant, t = 2: Score of the array: {2, 0, 0, 0}
At time instant, t = 3: Score of the array: {3, 1, 1, 0}
At time instant, t = 4: Score of the array: {4, 1, 1, 1}
At time instant, t = 5: Score of the array: {5, 1, 1, 1}
At time instant, t = 6: Score of the array: {6, 2, 2, 1}
Total score of the array after t = 5 = 6 + 2 + 2 + 1 = 11 ( > 10). Therefore, the
Input: arr[] = {2, 4, 3}, target = 20
Output: 20
方法:思路是使用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 现场工作专业课程和学生竞争性编程现场课程。