📌  相关文章
📜  最小化获得 N 所需的操作

📅  最后修改于: 2021-09-17 07:02:14             🧑  作者: Mango

给定一个整数N ,任务是获得N ,使用最少的操作次数从1开始。可以在一个步骤中执行的操作如下:

  • 将数字乘以 2。
  • 将数字乘以 3。
  • 在数字上加 1。

解释:

方法:
这个想法是使用动态规划的概念。请按照以下步骤操作:

  • 如果已知获得任何小于N 的数字的最小运算,则可以计算获得N 的最小运算。
  • 创建以下查找表:
  • 因此,对于任何数字x ,获得x所需的最小操作可以计算为:

下面是上述方法的实现:

C++
#include 
using namespace std;
 
// Function to find the minimum number
// of operations
int minOperations(int n)
{
    // Storing the minimum operations
    // to obtain all numbers up to N
    int dp[n + 1];
 
    // Initilal state
    dp[1] = 0;
 
    // Iterate for the remaining numbers
    for (int i = 2; i <= n; i++) {
 
        dp[i] = INT_MAX;
 
        // If the number can be obtained
        // by multiplication by 2
        if (i % 2 == 0) {
            int x = dp[i / 2];
            if (x + 1 < dp[i]) {
                dp[i] = x + 1;
            }
        }
        // If the number can be obtained
        // by multiplication by 3
        if (i % 3 == 0) {
            int x = dp[i / 3];
            if (x + 1 < dp[i]) {
                dp[i] = x + 1;
            }
        }
 
        // Obtaining the number by adding 1
        int x = dp[i - 1];
        if (x + 1 < dp[i]) {
            dp[i] = x + 1;
        }
    }
 
    // Return the minm operations
    // to obtain n
    return dp[n];
}
 
// Driver Code
int main()
{
    int n = 15;
    cout << minOperations(n);
    return 0;
}


Java
class GFG{
     
// Function to find the minimum number
// of operations
static int minOperations(int n)
{
     
    // Storing the minimum operations
    // to obtain all numbers up to N
    int dp[] = new int[n + 1];
 
    // Initilal state
    dp[1] = 0;
 
    // Iterate for the remaining numbers
    for(int i = 2; i <= n; i++)
    {
        dp[i] = Integer.MAX_VALUE;
 
        // If the number can be obtained
        // by multiplication by 2
        if (i % 2 == 0)
        {
            int x = dp[i / 2];
            if (x + 1 < dp[i])
            {
                dp[i] = x + 1;
            }
        }
         
        // If the number can be obtained
        // by multiplication by 3
        if (i % 3 == 0)
        {
            int x = dp[i / 3];
            if (x + 1 < dp[i])
            {
                dp[i] = x + 1;
            }
        }
 
        // Obtaining the number by adding 1
        int x = dp[i - 1];
        if (x + 1 < dp[i])
        {
            dp[i] = x + 1;
        }
    }
 
    // Return the minm operations
    // to obtain n
    return dp[n];
}
 
// Driver Code
public static void main (String []args)
{
    int n = 15;
     
    System.out.print( minOperations(n));
}
}
 
// This code is contributed by chitranayal


Python3
import sys
 
# Function to find the minimum number
# of operations
def minOperations(n):
     
    # Storing the minimum operations
    # to obtain all numbers up to N
    dp = [sys.maxsize] * (n + 1)
     
    # Initial state
    dp[1] = 0
     
    # Iterate for the remaining numbers
    for i in range(2, n + 1):
         
        # If the number can be obtained
        # by multiplication by 2
        if i % 2 == 0:
            x = dp[i // 2]
            if x + 1 < dp[i]:
                dp[i] = x + 1
         
        # If the number can be obtained
        # by multiplication by 3
        if i % 3 == 0:
            x = dp[i // 3]
            if x + 1 < dp[i]:
                dp[i] = x + 1
                 
        # Obtaining the number by adding 1
        x = dp[i - 1]
        if x + 1 < dp[i]:
            dp[i] = x + 1
             
    # Return the minimum operations
    # to obtain n
    return dp[n]
 
# Driver code
n = 15
 
print(minOperations(n))
         
# This code is contributed by Stuti Pathak


C#
using System;
class GFG{
      
// Function to find the minimum number
// of operations
static int minOperations(int n)
{
      
    // Storing the minimum operations
    // to obtain all numbers up to N
    int []dp = new int[n + 1];
  
    int x;
    // Initilal state
    dp[1] = 0;
  
    // Iterate for the remaining numbers
    for(int i = 2; i <= n; i++)
    {
        dp[i] = int.MaxValue;
  
        // If the number can be obtained
        // by multiplication by 2
        if (i % 2 == 0)
        {
            x = dp[i / 2];
            if (x + 1 < dp[i])
            {
                dp[i] = x + 1;
            }
        }
          
        // If the number can be obtained
        // by multiplication by 3
        if (i % 3 == 0)
        {
            x = dp[i / 3];
            if (x + 1 < dp[i])
            {
                dp[i] = x + 1;
            }
        }
  
        // Obtaining the number by adding 1
        x = dp[i - 1];
        if (x + 1 < dp[i])
        {
            dp[i] = x + 1;
        }
    }
  
    // Return the minm operations
    // to obtain n
    return dp[n];
}
  
// Driver Code
public static void Main (string []args)
{
    int n = 15;
      
    Console.Write(minOperations(n));
}
}
  
// This code is contributed by rock_cool


Javascript


输出:
4

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程