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

📅  最后修改于: 2021-04-22 04:12:05             🧑  作者: Mango

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

  • 将该数字乘以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)