📌  相关文章
📜  可以生成给定范围内所有值的最小硬币数量

📅  最后修改于: 2021-05-07 07:24:35             🧑  作者: Mango

给定整数N ,任务是找到创建范围[1,N]内的所有值所需的最小硬币数量。

例子:

Input: N = 5
Output: 3
The coins {1, 2, 4} can be used to generate 
all the values in the range [1, 5].
1 = 1
2 = 2
3 = 1 + 2
4 = 4
5 = 1 + 4

Input: N = 10
Output: 4

方法:该问题是硬币找零问题的一种变体,可以借助二进制数字解决。在上面的例子中,可以看出,创建所有1之间的值,以10,分母{1,2,4,8}需要其可以被重写为{2 0,2 1,2 2,2 3} 。可以使用以下算法来计算为值N创建所有值的最小硬币数量。

// A list which contains the sum of all previous 
// bit values including that bit value
list = [ 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023]
    // range = N
    for value in  list:
        if(value >= N):
            print(list.index(value) + 1)
            break

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
int index(vector vec, int value)
{
    vector::iterator it;
    it = find(vec.begin(), vec.end(), value);
    return (it - vec.begin());
}
 
// Function to return the count
// of minimum coins required
int findCount(int N)
{
 
    // To store the required sequence
    vector list;
    int sum = 0;
    int i;
 
    // Creating list of the sum of all
    // previous bit values including
    // that bit value
    for (i = 0; i < 20; i++) {
        sum += pow(2, i);
        list.push_back(sum);
    }
 
    for (i = 0; i < 20; i++) {
        if (list[i] >= N) {
            return (index(list, list[i]) + 1);
        }
    }
}
 
// Driver Code
int main()
{
    int N = 10;
    cout << findCount(N) << endl;
    return 0;
}
 
// This code is contributed by kanugargng


Java
// Java implementation of the approach
import java.util.*;
 
class GFG {
 
    // Function to return the count
    // of minimum coins required
    static int findCount(int N)
    {
        Vector list = new Vector();
        int sum = 0;
        int i;
 
        // Creating list of the sum of all
        // previous bit values including
        // that bit value
        for (i = 0; i < 20; i++) {
            sum += Math.pow(2, i);
            list.add(sum);
        }
 
        for (i = 0; i < 20; i++) {
            if ((int)list.get(i) >= N)
                return (list.indexOf(list.get(i)) + 1);
        }
        return 0;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 10;
 
        // Function Call to find count
        System.out.println(findCount(N));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
 
# Function to return the count
# of minimum coins required
 
 
def findCount(N):
 
    # To store the required sequence
    list = []
    sum = 0
 
    # Creating list of the sum of all
    # previous bit values including
    # that bit value
    for i in range(0, 20):
        sum += 2**i
        list.append(sum)
 
    for value in list:
        if(value >= N):
            return (list.index(value) + 1)
 
 
# Driver code
N = 10
print(findCount(N))


C#
// C# implementation of the above approach
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Function to return the count
    // of minimum coins required
    static int findCount(int N)
    {
        List list = new List();
        int sum = 0;
        int i;
 
        // Creating list of the sum of all
        // previous bit values including
        // that bit value
 
        for (i = 0; i < 20; i++) {
            sum += (int)Math.Pow(2, i);
            list.Add(sum);
        }
 
        for (i = 0; i < 20; i++) {
            if ((int)list[i] >= N)
                return (i + 1);
        }
        return 0;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int N = 10;
        Console.WriteLine(findCount(N));
    }
}
 
// This code is contributed by PrinciRaj1992


C++14
// C++ progarm to find minimum number of coins
#include
using namespace std;
 
// Function to find minimum number of coins
int findCount(int n)
{
    return log(n)/log(2)+1;
}
 
// Driver code
int main()
{
    int N = 10;
    cout << findCount(N) << endl;
    return 0;
}


Java
// Java progarm to find minimum number of coins
class GFG{
     
// Function to find minimum number of coins
static int findCount(int n)
{
    return (int)(Math.log(n) /
                 Math.log(2)) + 1;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 10;
     
    System.out.println(findCount(N));
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 progarm to find minimum number of coins
import math
 
# Function to find minimum number of coins
def findCount(n):
     
    return int(math.log(n, 2)) + 1
     
# Driver code
N = 10
 
print(findCount(N))
 
# This code is contributed by divyesh072019


C#
// C# progarm to find minimum number of coins
using System;
 
class GFG{
 
  // Function to find minimum number of coins
  static int findCount(int n)
  {
    return (int)(Math.Log(n) /
                 Math.Log(2)) + 1;
  }
 
  // Driver code
  public static void Main()
  {
    int N = 10;
 
    Console.Write(findCount(N));
  }
}
 
// This code is contributed by rutvik_56.


Javascript


输出
4

时间复杂度: O(n)

高效的方法:创建范围[1,N]中所有值所需的最小硬币数量为log(N)/ log(2)+ 1。

下面是上述方法的实现:

C++ 14

// C++ progarm to find minimum number of coins
#include
using namespace std;
 
// Function to find minimum number of coins
int findCount(int n)
{
    return log(n)/log(2)+1;
}
 
// Driver code
int main()
{
    int N = 10;
    cout << findCount(N) << endl;
    return 0;
}

Java

// Java progarm to find minimum number of coins
class GFG{
     
// Function to find minimum number of coins
static int findCount(int n)
{
    return (int)(Math.log(n) /
                 Math.log(2)) + 1;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 10;
     
    System.out.println(findCount(N));
}
}
 
// This code is contributed by divyeshrabadiya07

Python3

# Python3 progarm to find minimum number of coins
import math
 
# Function to find minimum number of coins
def findCount(n):
     
    return int(math.log(n, 2)) + 1
     
# Driver code
N = 10
 
print(findCount(N))
 
# This code is contributed by divyesh072019

C#

// C# progarm to find minimum number of coins
using System;
 
class GFG{
 
  // Function to find minimum number of coins
  static int findCount(int n)
  {
    return (int)(Math.Log(n) /
                 Math.Log(2)) + 1;
  }
 
  // Driver code
  public static void Main()
  {
    int N = 10;
 
    Console.Write(findCount(N));
  }
}
 
// This code is contributed by rutvik_56.

Java脚本


输出
4

时间复杂度: O(log(n))