给定一个数字N ,任务是通过用给定数字中存在的任何数字减去给定数字来找到将数字N减少为零所需的最小操作次数。
例子:
Input: N = 4
Output: 1
Explanation:
Here 4 is the only digit present hence 4 – 4 = 0 and only one operation is required.
Input: N = 17
Output: 3
Explanation:
The given integer is 17 and the steps of reduction are:
17 -> 17 – 7 = 10
10 -> 10 – 1 = 9
9 -> 9 – 9 = 0.
Hence 3 operations are required.
方法:这个问题可以用动态规划解决。
对于任何给定数目N,遍历N中的每个位,并递归地通过由一个减去每个数字一直到Ñ降低到0检查。但是执行递归会使方法的时间复杂度呈指数级增长。
因此,这个想法是使用大小为(N + 1)的数组(比如dp[] ),这样dp[i]将存储将i减少到 0所需的最少操作数。
对于数字N中的每个数字x ,使用的递推关系由下式给出:
dp[i] = min(dp[i], dp[i-x] + 1),
where dp[i] will store the minimum number of operations needed to reduce i to 0.
我们将采用自下而上的方式从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)