给定一个正整数N,则任务是找到通过递减N乘3或除以N乘5至N转换为2任一所需的操作的最小数量,如果N是由5整除。如果不可能将N减少到2 ,则打印“-1” 。
例子:
Input: N =10
Output: 1
Explanation:
Following are the operations performed to reduce N to 2:
- Dividing N by 5, reduces N to 10/5 = 2.
After the above operations, N is reduced to 2. Therefore, the minimum number of operations required is 1.
Input: N = 25
Output: 2
方法:给定的问题可以通过使用动态规划来解决,其思想是从2开始迭代,并以相反的方式执行这两个操作,即不减,执行3的加法,而不是除法,执行与5 的乘法每个状态并在数组dp[] 中存储N 的每个可能值的最小操作数。
如果达到N的值,则打印dp[N]的值作为最小操作次数。否则,打印-1 。请按照以下步骤解决问题:
- 初始化一个辅助数组,比如大小为(N + 1) 的dp[] ,并用INT_MAX初始化所有数组元素。
- 将dp[2]的值设置为0 。
- 迭代范围[0, N] ,并将dp[i]的值更新为:
- dp[i * 5] = min(dp[i * 5], dp[i] + 1)。
- dp[i + 3] = min(dp[i + 3], dp[i] + 1)。
- 如果dp[N] 的值为INT_MAX ,则打印-1 。否则,打印dp[N]作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum number
// of operations to reduce N to 2 by
// dividing N by 5 or decrementing by 3
int minimumOperations(int N)
{
// Initialize the dp array
int dp[N + 1];
int i;
// Initialize the array dp[]
for (int i = 0; i <= N; i++) {
dp[i] = 1e9;
}
// For N = 2 number of operations
// needed is zero
dp[2] = 0;
// Itrating over the range [1, N]
for (i = 2; i <= N; i++) {
// If it's not possible to
// create current N
if (dp[i] == 1e9)
continue;
// Multiply with 5
if (i * 5 <= N) {
dp[i * 5] = min(dp[i * 5],
dp[i] + 1);
}
// Adding the value 3
if (i + 3 <= N) {
dp[i + 3] = min(dp[i + 3],
dp[i] + 1);
}
}
// Checking if not possible to
// make the number as 2
if (dp[N] == 1e9)
return -1;
// Return the minimum number
// of operations
return dp[N];
}
// Driver Code
int main()
{
int N = 25;
cout << minimumOperations(N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find the minimum number
// of operations to reduce N to 2 by
// dividing N by 5 or decrementing by 3
static int minimumOperations(int N)
{
// Initialize the dp array
int[] dp = new int[N + 1];
int i;
// Initialize the array dp[]
for (i = 0; i <= N; i++) {
dp[i] = (int)1e9;
}
// For N = 2 number of operations
// needed is zero
dp[2] = 0;
// Itrating over the range [1, N]
for (i = 2; i <= N; i++) {
// If it's not possible to
// create current N
if (dp[i] == (int)1e9)
continue;
// Multiply with 5
if (i * 5 <= N) {
dp[i * 5] = Math.min(dp[i * 5], dp[i] + 1);
}
// Adding the value 3
if (i + 3 <= N) {
dp[i + 3] = Math.min(dp[i + 3], dp[i] + 1);
}
}
// Checking if not possible to
// make the number as 2
if (dp[N] == 1e9)
return -1;
// Return the minimum number
// of operations
return dp[N];
}
// Driver Code
public static void main(String[] args)
{
int N = 25;
System.out.println(minimumOperations(N));
}
}
// This code is contributed by Potta Lokesh
C#
// C# program for above approach
using System;
class GFG{
// Function to find the minimum number
// of operations to reduce N to 2 by
// dividing N by 5 or decrementing by 3
static int minimumOperations(int N)
{
// Initialize the dp array
int[] dp = new int[N + 1];
int i;
// Initialize the array dp[]
for (i = 0; i <= N; i++) {
dp[i] = (int)1e9;
}
// For N = 2 number of operations
// needed is zero
dp[2] = 0;
// Itrating over the range [1, N]
for (i = 2; i <= N; i++) {
// If it's not possible to
// create current N
if (dp[i] == (int)1e9)
continue;
// Multiply with 5
if (i * 5 <= N) {
dp[i * 5] = Math.Min(dp[i * 5], dp[i] + 1);
}
// Adding the value 3
if (i + 3 <= N) {
dp[i + 3] = Math.Min(dp[i + 3], dp[i] + 1);
}
}
// Checking if not possible to
// make the number as 2
if (dp[N] == 1e9)
return -1;
// Return the minimum number
// of operations
return dp[N];
}
// Driver Code
public static void Main(String[] args)
{
int N = 25;
Console.Write(minimumOperations(N));
}
}
// This code is contributed by sanjoy_62.
Javascript
Python3
# Python 3 program for the above approach
# Function to find the minimum number
# of operations to reduce N to 2 by
# dividing N by 5 or decrementing by 3
def minimumOperations(N):
# Initialize the dp array
dp = [0 for i in range(N + 1)]
# Initialize the array dp[]
for i in range(N+1):
dp[i] = 1000000000
# For N = 2 number of operations
# needed is zero
dp[2] = 0
# Itrating over the range [1, N]
for i in range(2,N+1,1):
# If it's not possible to
# create current N
if (dp[i] == 1000000000):
continue
# Multiply with 5
if (i * 5 <= N):
dp[i * 5] = min(dp[i * 5], dp[i] + 1)
# Adding the value 3
if (i + 3 <= N):
dp[i + 3] = min(dp[i + 3], dp[i] + 1)
# Checking if not possible to
# make the number as 2
if (dp[N] == 1000000000):
return -1
# Return the minimum number
# of operations
return dp[N]
# Driver Code
if __name__ == '__main__':
N = 25
print(minimumOperations(N))
输出:
2
时间复杂度: O(N)
辅助空间: O(N)