📌  相关文章
📜  将数字减少到 2 所需的最小给定操作数

📅  最后修改于: 2021-09-22 09:52:39             🧑  作者: Mango

给定一个正整数N ,任务是通过执行以下操作的最少次数将N减少到2

  • 操作1:除以5 N,如果N是由5整除。
  • 操作 2:N 中减去3

如果不可能,则打印-1

例子:

朴素的方法:这个想法是递归计算所需的最小步骤数。

  • 如果该数字不能被 5 整除,则从 n 中减去 3,然后对 n 的修改值重复计算,结果加 1。
  • 否则进行两次递归调用,一次是从 n 中减去 3,另一次是将 n 除以 5,然后返回操作次数最少的一次,结果加 1。

时间复杂度: O(2 n )
辅助空间: O(1)

高效的方法:为了优化上述方法,思想是使用动态规划。请按照以下步骤解决此问题。

  • 创建一个数组,比如dp[n+1]来存储最小操作并使用 INT_MAX 初始化所有条目,其中dp[i]存储从i达到 2 所需的最小步数。
  • 通过将dp[2]初始化为0 来处理基本情况。
  • 使用变量i在范围[2, n] 中迭代
    • 如果i*5 ≤ n ,则将dp[i*5]更新为dp[i*5]dp[i]+1 的最小值。
    • 如果i+3 ≤ n的值,则将dp[i+3]更新为dp[i+3]dp[i]+1 的最小值。
  • 打印dp[n]的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum number
// of operations required to reduce n to 2
int findMinOperations(int n)
{
    // Initialize a dp array
    int i, dp[n + 1];
 
    for (i = 0; i < n + 1; i++) {
        dp[i] = 999999;
    }
 
    // Handle the base case
    dp[2] = 0;
 
    // Iterate in the range [2, n]
    for (i = 2; i < n + 1; i++) {
 
        // Check if i * 5 <= n
        if (i * 5 <= n) {
 
            dp[i * 5] = min(
dp[i * 5], dp[i] + 1);
        }
 
        // Check if i + 3 <= n
        if (i + 3 <= n) {
 
            dp[i + 3] = min(
dp[i + 3], dp[i] + 1);
        }
    }
 
    // Return the result
    return dp[n];
}
 
// Driver code
int main()
{
    // Given Input
    int n = 28;
 
    // Function Call
    int m = findMinOperations(n);
 
    // Print the result
    if (m != 9999)
        cout << m;
    else
        cout << -1;
 
    return 0;
}


Java
// Java program for the above approach
public class GFG {
 
    // Function to find the minimum number
    // of operations required to reduce n to 2
    static int findMinOperations(int n)
    {
        // Initialize a dp array
        int i = 0;
        int dp[] = new int[n + 1];
 
        for (i = 0; i < n + 1; i++) {
            dp[i] = 999999;
        }
 
        // Handle the base case
        dp[2] = 0;
 
        // Iterate in the range [2, n]
        for (i = 2; i < n + 1; i++) {
 
            // Check if i * 5 <= n
            if (i * 5 <= n) {
 
                dp[i * 5] = Math.min(dp[i * 5], dp[i] + 1);
            }
 
            // Check if i + 3 <= n
            if (i + 3 <= n) {
 
                dp[i + 3] = Math.min(dp[i + 3], dp[i] + 1);
            }
        }
 
        // Return the result
        return dp[n];
    }
 
    // Driver code
    public static void main(String[] args)
    {
       
      // Given Input
        int n = 28;
 
        // Function Call
        int m = findMinOperations(n);
 
        // Print the result
        if (m != 9999)
            System.out.println(m);
        else
            System.out.println(-1);
    }
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
 
# Function to find the minimum number
# of operations required to reduce n to 2
def findMinOperations(n):
     
    # Initialize a dp array
    dp = [0 for i in range(n + 1)]
 
    for i in range(n + 1):
        dp[i] = 999999
 
    # Handle the base case
    dp[2] = 0
 
    # Iterate in the range [2, n]
    for i in range(2, n + 1):
         
        # Check if i * 5 <= n
        if (i * 5 <= n):
            dp[i * 5] = min(dp[i * 5],
                            dp[i] + 1)
                             
        # Check if i + 3 <= n
        if (i + 3 <= n):
            dp[i + 3] = min(dp[i + 3],
                           dp[i] + 1)
 
    # Return the result
    return dp[n]
 
# Driver code
if __name__ == '__main__':
     
    # Given Input
    n = 28
 
    # Function Call
    m = findMinOperations(n)
 
    # Print the result
    if (m != 9999):
        print (m)
    else:
        print (-1)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the minimum number
// of operations required to reduce n to 2
static int findMinOperations(int n)
{
     
    // Initialize a dp array
    int i;
    int []dp = new int[n + 1];
 
    for(i = 0; i < n + 1; i++)
    {
        dp[i] = 999999;
    }
 
    // Handle the base case
    dp[2] = 0;
 
    // Iterate in the range [2, n]
    for(i = 2; i < n + 1; i++)
    {
         
        // Check if i * 5 <= n
        if (i * 5 <= n)
        {
             
            dp[i * 5] = Math.Min(dp[i * 5],
                                dp[i] + 1);
        }
 
        // Check if i + 3 <= n
        if (i + 3 <= n)
        {
             
            dp[i + 3] = Math.Min(dp[i + 3],
                                dp[i] + 1);
        }
    }
 
    // Return the result
    return dp[n];
}
 
// Driver code
public static void Main()
{
     
    // Given Input
    int n = 28;
 
    // Function Call
    int m = findMinOperations(n);
 
    // Print the result
    if (m != 9999)
        Console.Write(m);
    else
        Console.Write(-1);
}
}
     
// This code is contributed by SURENDRA_GANGWAR


Javascript


输出:
3

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