📌  相关文章
📜  获得所有硬币的最低成本,每个硬币允许额外的 k 个硬币

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

给你一个不同面额的 N 个硬币的清单。您可以支付相当于任何 1 个硬币的金额并可以获得该硬币。此外,一旦您支付了一个硬币,我们最多可以再选择 K 个硬币,并且可以免费获得这些硬币。任务是找到在给定的 K 值下获得所有 N 个硬币所需的最小数量。

例子 :

Input : coin[] = {100, 20, 50, 10, 2, 5}, 
        k = 3
Output : 7

Input : coin[] = {1, 2, 5, 10, 20, 50}, 
        k = 3
Output : 3

根据问题,我们可以看到,以 1 个硬币的成本,我们最多可以获得 K+1 个硬币。因此,为了获得所有的 n 个硬币,我们将选择 ceil(n/(k+1)) 个硬币,如果我们选择最小的 ceil(n/(k+1)),则选择硬币的成本将是最低的(贪婪的方法)。可以通过简单地按升序对所有 N 个值进行排序来找到最小的 ceil(n/(k+1)) 硬币。
如果我们应该检查时间复杂度 (n log n) 用于排序元素,而 (k) 用于添加总量。所以,最后时间复杂度:O(n log n)。

C++
// C++ program to acquire all n coins
#include
using namespace std;
 
// function to calculate min cost
int minCost(int coin[], int n, int k)
{
    // sort the coins value
    sort(coin, coin + n);
 
    // calculate no. of
    // coins needed
    int coins_needed = ceil(1.0 * n /
                            (k + 1));
 
    // calculate sum of
    // all selected coins
    int ans = 0;
    for (int i = 0; i <= coins_needed - 1;
                                      i++)
        ans += coin[i];
     
    return ans;
}
 
// Driver Code
int main()
{
    int coin[] = {8, 5, 3, 10,
                  2, 1, 15, 25};
    int n = sizeof(coin) / sizeof(coin[0]);
    int k = 3;
    cout << minCost(coin, n, k);
    return 0;
}


Java
// Java program to acquire
// all n coins
import java.util.Arrays;
 
class GFG
{
     
    // function to calculate min cost
    static int minCost(int coin[],
                       int n, int k)
    {
         
        // sort the coins value
        Arrays.sort(coin);
 
        // calculate no. of
        // coins needed
        int coins_needed = (int)Math.ceil(1.0 *
                                  n / (k + 1));
 
        // calculate sum of
        // all selected coins
        int ans = 0;
         
        for (int i = 0; i <= coins_needed - 1;
                                          i++)
            ans += coin[i];
 
        return ans;
    }
     
    // Driver code
    public static void main(String arg[])
    {
        int coin[] = { 8, 5, 3, 10,
                       2, 1, 15, 25 };
        int n = coin.length;
        int k = 3;
         
        System.out.print(minCost(coin, n, k));
    }
}
 
// This code is contributed
// by Anant Agarwal.


Python3
# Python3 program to
# acquire all n coins
 
import math
 
# function to calculate min cost
def minCost(coin, n, k):
 
    # sort the coins value
    coin.sort()
 
    # calculate no. of
    # coins needed
    coins_needed = math.ceil(1.0 * n //
                            (k + 1));
 
    # calculate sum of all
    # selected coins
    ans = 0
    for i in range(coins_needed - 1 + 1):
        ans += coin[i]
     
    return ans
 
# Driver code
coin = [8, 5, 3, 10,
        2, 1, 15, 25]
n = len(coin)
k = 3
 
print(minCost(coin, n, k))
 
# This code is contributed
# by Anant Agarwal.


C#
// C# program to acquire all n coins
using System;
 
class GFG
{
     
    // function to calculate min cost
    static int minCost(int []coin,
                       int n, int k)
    {
        // sort the coins value
        Array.Sort(coin);
 
        // calculate no. of coins needed
        int coins_needed = (int)Math.Ceiling(1.0 *
                                     n / (k + 1));
 
        // calculate sum of
        // all selected coins
        int ans = 0;
         
        for (int i = 0; i <= coins_needed - 1; i++)
            ans += coin[i];
 
        return ans;
    }
     
    // Driver code
    public static void Main()
    {
        int []coin = {8, 5, 3, 10,
                      2, 1, 15, 25};
        int n = coin.Length;
        int k = 3;
         
        // Function calling
        Console.Write(minCost(coin, n, k));
    }
}
 
// This code is contributed
// by nitin mittal.


PHP


Javascript


C++
// C++ program to acquire all
// n coins at minimum cost
// with multiple values of k.
#include
using namespace std;
 
// Converts coin[] to prefix sum array
void preprocess(int coin[], int n)
{
    // sort the coins value
    sort(coin, coin + n);
 
    // Maintain prefix sum array
    for (int i = 1; i <= n - 1; i++)
        coin[i] += coin[i - 1];
}
 
// Function to calculate min
// cost when we can get k extra
// coins after paying cost of one.
int minCost(int coin[], int n, int k)
{
    // calculate no. of coins needed
    int coins_needed = ceil(1.0 * n / (k + 1));
 
    // return sum of from prefix array
    return coin[coins_needed - 1];
}
 
// Driver Code
int main()
{
    int coin[] = {8, 5, 3, 10,
                  2, 1, 15, 25};
    int n = sizeof(coin) / sizeof(coin[0]);
    preprocess(coin, n);
    int k = 3;
    cout << minCost(coin, n, k) << endl;
    k = 7;
    cout << minCost(coin, n, k) << endl;
    return 0;
}


Java
// C# program to acquire all n coins at
// minimum cost with multiple values of k.
import java .io.*;
import java.util.Arrays;
 
public class GFG {
     
    // Converts coin[] to prefix sum array
    static void preprocess(int []coin, int n)
    {
         
        // sort the coins value
        Arrays.sort(coin);
     
        // Maintain prefix sum array
        for (int i = 1; i <= n - 1; i++)
            coin[i] += coin[i - 1];
    }
     
    // Function to calculate min cost when we
    // can get k extra coins after paying
    // cost of one.
    static int minCost(int []coin, int n, int k)
    {
         
        // calculate no. of coins needed
        int coins_needed =(int) Math.ceil(1.0
                                * n / (k + 1));
     
        // return sum of from prefix array
        return coin[coins_needed - 1];
    }
     
    // Driver Code
    static public void main (String[] args)
    {
        int []coin = {8, 5, 3, 10, 2, 1, 15, 25};
        int n = coin.length;
         
        preprocess(coin, n);
         
        int k = 3;
        System.out.println(minCost(coin, n, k));
         
        k = 7;
        System.out.println( minCost(coin, n, k));
    }
}
 
// This code is contributed by anuj_67.


Python3
# Python3 program to acquire all n coins at
# minimum cost with multiple values of k.
import math as mt
 
# Converts coin[] to prefix sum array
def preprocess(coin, n):
 
    # sort the coins values
    coin.sort()
     
    # maintain prefix sum array
    for i in range(1, n):
        coin[i] += coin[i - 1]
     
# Function to calculate min cost when we can 
# get k extra coins after paying cost of one.
def minCost(coin, n, k):
     
    # calculate no. of coins needed
    coins_needed = mt.ceil(1.0 * n / (k + 1))
     
    # return sum of from prefix array
    return coin[coins_needed - 1]
     
# Driver code
coin = [8, 5, 3, 10, 2, 1, 15, 25]
 
n = len(coin)
 
preprocess(coin, n)
k = 3
 
print(minCost(coin, n, k))
 
k = 7
 
print(minCost(coin, n, k))
 
# This code is contributed
# by Mohit kumar 29


C#
// C# program to acquire all n coins at
// minimum cost with multiple values of k.
using System;
 
public class GFG {
     
    // Converts coin[] to prefix sum array
    static void preprocess(int []coin, int n)
    {
         
        // sort the coins value
        Array.Sort(coin);
     
        // Maintain prefix sum array
        for (int i = 1; i <= n - 1; i++)
            coin[i] += coin[i - 1];
    }
     
    // Function to calculate min cost when we
    // can get k extra coins after paying
    // cost of one.
    static int minCost(int []coin, int n, int k)
    {
         
        // calculate no. of coins needed
        int coins_needed =(int) Math.Ceiling(1.0
                                 * n / (k + 1));
     
        // return sum of from prefix array
        return coin[coins_needed - 1];
    }
     
    // Driver Code
    static public void Main ()
    {
        int []coin = {8, 5, 3, 10, 2, 1, 15, 25};
        int n = coin.Length;
         
        preprocess(coin, n);
         
        int k = 3;
        Console.WriteLine(minCost(coin, n, k));
         
        k = 7;
        Console.WriteLine( minCost(coin, n, k));
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


输出 :

3

请注意,有更有效的方法可以找到给定数量的最小值。例如,数组中m个最大(或最小)元素的方法6可以在(nm)Log m + m Log m中找到第m个最小元素)。

如何处理单个预定义数组的多个查询?
在这种情况下,如果您被要求为 K 的多个值找到上述答案,您必须快速计算它,并且我们的时间复杂度随着对 k 的查询次数而增加。为了服务的目的,我们可以在对所有 N 个值进行排序后维护一个前缀和数组,并且可以轻松快速地回答查询。
认为

C++

// C++ program to acquire all
// n coins at minimum cost
// with multiple values of k.
#include
using namespace std;
 
// Converts coin[] to prefix sum array
void preprocess(int coin[], int n)
{
    // sort the coins value
    sort(coin, coin + n);
 
    // Maintain prefix sum array
    for (int i = 1; i <= n - 1; i++)
        coin[i] += coin[i - 1];
}
 
// Function to calculate min
// cost when we can get k extra
// coins after paying cost of one.
int minCost(int coin[], int n, int k)
{
    // calculate no. of coins needed
    int coins_needed = ceil(1.0 * n / (k + 1));
 
    // return sum of from prefix array
    return coin[coins_needed - 1];
}
 
// Driver Code
int main()
{
    int coin[] = {8, 5, 3, 10,
                  2, 1, 15, 25};
    int n = sizeof(coin) / sizeof(coin[0]);
    preprocess(coin, n);
    int k = 3;
    cout << minCost(coin, n, k) << endl;
    k = 7;
    cout << minCost(coin, n, k) << endl;
    return 0;
}

Java

// C# program to acquire all n coins at
// minimum cost with multiple values of k.
import java .io.*;
import java.util.Arrays;
 
public class GFG {
     
    // Converts coin[] to prefix sum array
    static void preprocess(int []coin, int n)
    {
         
        // sort the coins value
        Arrays.sort(coin);
     
        // Maintain prefix sum array
        for (int i = 1; i <= n - 1; i++)
            coin[i] += coin[i - 1];
    }
     
    // Function to calculate min cost when we
    // can get k extra coins after paying
    // cost of one.
    static int minCost(int []coin, int n, int k)
    {
         
        // calculate no. of coins needed
        int coins_needed =(int) Math.ceil(1.0
                                * n / (k + 1));
     
        // return sum of from prefix array
        return coin[coins_needed - 1];
    }
     
    // Driver Code
    static public void main (String[] args)
    {
        int []coin = {8, 5, 3, 10, 2, 1, 15, 25};
        int n = coin.length;
         
        preprocess(coin, n);
         
        int k = 3;
        System.out.println(minCost(coin, n, k));
         
        k = 7;
        System.out.println( minCost(coin, n, k));
    }
}
 
// This code is contributed by anuj_67.

蟒蛇3

# Python3 program to acquire all n coins at
# minimum cost with multiple values of k.
import math as mt
 
# Converts coin[] to prefix sum array
def preprocess(coin, n):
 
    # sort the coins values
    coin.sort()
     
    # maintain prefix sum array
    for i in range(1, n):
        coin[i] += coin[i - 1]
     
# Function to calculate min cost when we can 
# get k extra coins after paying cost of one.
def minCost(coin, n, k):
     
    # calculate no. of coins needed
    coins_needed = mt.ceil(1.0 * n / (k + 1))
     
    # return sum of from prefix array
    return coin[coins_needed - 1]
     
# Driver code
coin = [8, 5, 3, 10, 2, 1, 15, 25]
 
n = len(coin)
 
preprocess(coin, n)
k = 3
 
print(minCost(coin, n, k))
 
k = 7
 
print(minCost(coin, n, k))
 
# This code is contributed
# by Mohit kumar 29

C#

// C# program to acquire all n coins at
// minimum cost with multiple values of k.
using System;
 
public class GFG {
     
    // Converts coin[] to prefix sum array
    static void preprocess(int []coin, int n)
    {
         
        // sort the coins value
        Array.Sort(coin);
     
        // Maintain prefix sum array
        for (int i = 1; i <= n - 1; i++)
            coin[i] += coin[i - 1];
    }
     
    // Function to calculate min cost when we
    // can get k extra coins after paying
    // cost of one.
    static int minCost(int []coin, int n, int k)
    {
         
        // calculate no. of coins needed
        int coins_needed =(int) Math.Ceiling(1.0
                                 * n / (k + 1));
     
        // return sum of from prefix array
        return coin[coins_needed - 1];
    }
     
    // Driver Code
    static public void Main ()
    {
        int []coin = {8, 5, 3, 10, 2, 1, 15, 25};
        int n = coin.Length;
         
        preprocess(coin, n);
         
        int k = 3;
        Console.WriteLine(minCost(coin, n, k));
         
        k = 7;
        Console.WriteLine( minCost(coin, n, k));
    }
}
 
// This code is contributed by anuj_67.

PHP


Javascript


输出 :

3
1

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