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

📅  最后修改于: 2021-04-29 17:56:14             🧑  作者: 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


输出 :

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.

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


输出 :

3
1