📌  相关文章
📜  通过从N减去任何数字来将N减少为0的最小操作数

📅  最后修改于: 2021-06-26 20:33:03             🧑  作者: Mango

给定数字N ,任务是找到将数字N减为零所需的最小操作数,方法是将给定数字减去其中存在的任何数字。
例子:

方法:可以使用动态编程解决此问题。
对于任何给定数目N,遍历N中的每个位,并递归地通过由一个减去每个数字一直到Ñ降低到0检查。但是执行递归将使方法的时间复杂度成指数增长。
因此,这个想法是使用大小为(N + 1)的数组(例如dp [] ),以便dp [i]将存储将i减少为0所需的最小操作数。
对于数字N中的每个数字x ,使用的递归关系由下式给出:

我们将采用自下而上的方式0填充数组DP []为N,然后DP [N]将会给运算的最小数量。
下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to reduce an integer N
// to Zero in minimum operations by
// removing digits from N
int reduceZero(int N)
{
    // Initialise dp[] to steps
    vector dp(N + 1, 1e9);
 
    dp[0] = 0;
 
    // Iterate for all elements
    for (int i = 0; i <= N; i++) {
 
        // For each digit in number i
        for (char c : to_string(i)) {
 
            // Either select the number
            // or do not select it
            dp[i] = min(dp[i],
                        dp[i - (c - '0')]
                            + 1);
        }
    }
 
    // dp[N] will give minimum
    // step for N
    return dp[N];
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 25;
 
    // Function Call
    cout << reduceZero(N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to reduce an integer N
// to Zero in minimum operations by
// removing digits from N
static int reduceZero(int N)
{
    // Initialise dp[] to steps
    int []dp = new int[N + 1];
    for (int i = 0; i <= N; i++)
        dp[i] = (int) 1e9;
    dp[0] = 0;
 
    // Iterate for all elements
    for (int i = 0; i <= N; i++)
    {
 
        // For each digit in number i
        for (char c : String.valueOf(i).toCharArray())
        {
 
            // Either select the number
            // or do not select it
            dp[i] = Math.min(dp[i],
                             dp[i - (c - '0')] + 1);
        }
    }
 
    // dp[N] will give minimum
    // step for N
    return dp[N];
}
 
// Driver Code
public static void main(String[] args)
{
    // Given Number
    int N = 25;
 
    // Function Call
    System.out.print(reduceZero(N));
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 program for the above approach
 
# Function to reduce an integer N
# to Zero in minimum operations by
# removing digits from N
def reduceZero(N):
     
    # Initialise dp[] to steps
    dp = [1e9 for i in range(N + 1)]
 
    dp[0] = 0
 
    # Iterate for all elements
    for i in range(N + 1):
         
        # For each digit in number i
        for c in str(i):
             
            # Either select the number
            # or do not select it
            dp[i] = min(dp[i],
                        dp[i - (ord(c) - 48)] + 1)
 
    # dp[N] will give minimum
    # step for N
    return dp[N]
 
# Driver Code
N = 25
 
# Function Call
print(reduceZero(N))
 
# This code is contributed by Sanjit_Prasad


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to reduce an integer N
// to Zero in minimum operations by
// removing digits from N
static int reduceZero(int N)
{
    // Initialise []dp to steps
    int []dp = new int[N + 1];
    for (int i = 0; i <= N; i++)
        dp[i] = (int) 1e9;
    dp[0] = 0;
 
    // Iterate for all elements
    for (int i = 0; i <= N; i++)
    {
 
        // For each digit in number i
        foreach (char c in String.Join("", i).ToCharArray())
        {
 
            // Either select the number
            // or do not select it
            dp[i] = Math.Min(dp[i],
                             dp[i - (c - '0')] + 1);
        }
    }
 
    // dp[N] will give minimum
    // step for N
    return dp[N];
}
 
// Driver Code
public static void Main(String[] args)
{
    // Given Number
    int N = 25;
 
    // Function Call
    Console.Write(reduceZero(N));
}
}
 
// This code is contributed by amal kumar choubey


Javascript


输出:
5

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

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。