📌  相关文章
📜  将N减少到0所需的最小操作数

📅  最后修改于: 2021-04-29 15:04:19             🧑  作者: Mango

给定整数N ,任务是通过执行以下两个操作来计算将N的值减小为0所需的最少步骤:

  • 考虑整数AB ,其中N = A * B (A!= 1和B!= 1),将N简化为min(A,B)
  • N的值减1

例子 :

天真的方法:这个想法是使用动态编程的概念。请按照以下步骤解决问题:

  • 解决此问题的简单方法是将N替换为每个可能的值,直到它不为0。
  • N达到0时,将动作计数与到目前为止获得的最小值进行比较,以获得最佳答案。
  • 最后,打印计算出的最小步骤

因此,关系为:

下面是上述方法的实现:

C++
Recursive tree for N = 4 is
                  4
                /   \
               3     2(2*2)
               |      |
               2      1
               |      |
               1      0
               |
               0


Java
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to count the minimum
// steps required to reduce n
int downToZero(int n)
{
    // Base case
    if (n <= 3)
        return n;
 
    // Allocate memory for storing
    // intermediate results
    vector dp(n + 1, -1);
 
    // Store base values
    dp[0] = 0;
    dp[1] = 1;
    dp[2] = 2;
    dp[3] = 3;
 
    // Stores square root
    // of each number
    int sqr;
    for (int i = 4; i <= n; i++) {
 
        // Compute square root
        sqr = sqrt(i);
 
        int best = INT_MAX;
 
        // Use rule 1 to find optimized
        // answer
        while (sqr > 1) {
 
            // Check if it perfectly divides n
            if (i % sqr == 0) {
                best = min(best, 1 + dp[sqr]);
            }
 
            sqr--;
        }
 
        // Use of rule 2 to find
        // the optimized answer
        best = min(best, 1 + dp[i - 1]);
 
        // Store computed value
        dp[i] = best;
    }
 
    // Return answer
    return dp[n];
}
 
// Driver Code
int main()
{
    int n = 4;
    cout << downToZero(n);
    return 0;
}


Python3
// Java program to implement
// the above approach
class GFG{
 
// Function to count the minimum
// steps required to reduce n
static int downToZero(int n)
{
     
    // Base case
    if (n <= 3)
        return n;
 
    // Allocate memory for storing
    // intermediate results
    int []dp = new int[n + 1];
    for(int i = 0; i < n + 1; i++)
        dp[i] = -1;
         
    // Store base values
    dp[0] = 0;
    dp[1] = 1;
    dp[2] = 2;
    dp[3] = 3;
 
    // Stores square root
    // of each number
    int sqr;
    for(int i = 4; i <= n; i++)
    {
         
        // Compute square root
        sqr = (int)Math.sqrt(i);
 
        int best = Integer.MAX_VALUE;
 
        // Use rule 1 to find optimized
        // answer
        while (sqr > 1)
        {
 
            // Check if it perfectly divides n
            if (i % sqr == 0)
            {
                best = Math.min(best, 1 + dp[sqr]);
            }
            sqr--;
        }
 
        // Use of rule 2 to find
        // the optimized answer
        best = Math.min(best, 1 + dp[i - 1]);
 
        // Store computed value
        dp[i] = best;
    }
 
    // Return answer
    return dp[n];
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 4;
    System.out.print(downToZero(n));
}
}
 
// This code is contributed by amal kumar choubey


C#
# Python3 program to implement
# the above approach
import math
import sys
 
# Function to count the minimum
# steps required to reduce n
def downToZero(n):
 
    # Base case
    if (n <= 3):
        return n
 
    # Allocate memory for storing
    # intermediate results
    dp = [-1] * (n + 1)
 
    # Store base values
    dp[0] = 0
    dp[1] = 1
    dp[2] = 2
    dp[3] = 3
 
    # Stores square root
    # of each number
    for i in range(4, n + 1):
 
        # Compute square root
        sqr = (int)(math.sqrt(i))
 
        best = sys.maxsize
 
        # Use rule 1 to find optimized
        # answer
        while (sqr > 1):
 
            # Check if it perfectly divides n
            if (i % sqr == 0):
                best = min(best, 1 + dp[sqr])
             
            sqr -= 1
 
        # Use of rule 2 to find
        # the optimized answer
        best = min(best, 1 + dp[i - 1])
 
        # Store computed value
        dp[i] = best
 
    # Return answer
    return dp[n]
 
# Driver Code
if __name__ == "__main__":
     
    n = 4
 
    print(downToZero(n))
 
# This code is contributed by chitranayal


Javascript
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to count the minimum
// steps required to reduce n
static int downToZero(int n)
{
     
    // Base case
    if (n <= 3)
        return n;
 
    // Allocate memory for storing
    // intermediate results
    int []dp = new int[n + 1];
    for(int i = 0; i < n + 1; i++)
        dp[i] = -1;
         
    // Store base values
    dp[0] = 0;
    dp[1] = 1;
    dp[2] = 2;
    dp[3] = 3;
 
    // Stores square root
    // of each number
    int sqr;
    for(int i = 4; i <= n; i++)
    {
         
        // Compute square root
        sqr = (int)Math.Sqrt(i);
 
        int best = int.MaxValue;
 
        // Use rule 1 to find optimized
        // answer
        while (sqr > 1)
        {
 
            // Check if it perfectly divides n
            if (i % sqr == 0)
            {
                best = Math.Min(best, 1 + dp[sqr]);
            }
            sqr--;
        }
 
        // Use of rule 2 to find
        // the optimized answer
        best = Math.Min(best, 1 + dp[i - 1]);
 
        // Store computed value
        dp[i] = best;
    }
 
    // Return answer
    return dp[n];
}
 
// Driver Code
public static void Main(String[] args)
{
    int n = 4;
    Console.Write(downToZero(n));
}
}
 
// This code is contributed by amal kumar choubey


C++


Java
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the minimum
// steps required to reduce n
int downToZero(int n)
{
    // Base case
    if (n <= 3)
        return n;
 
    // Return answer based on
    // parity of n
    return n % 2 == 0 ? 3 : 4;
}
 
// Driver Code
int main()
{
    int n = 4;
    cout << downToZero(n);
 
    return 0;
}


Python3
// Java Program to implement
// the above approach
class GFG{
  
// Function to find the minimum
// steps required to reduce n
static int downToZero(int n)
{
    // Base case
    if (n <= 3)
        return n;
  
    // Return answer based on
    // parity of n
    return n % 2 == 0 ? 3 : 4;
}
  
// Driver Code
public static void main(String[] args)
{
    int n = 4;
    System.out.println(downToZero(n));
}
}
 
// This code is contributed by rock_cool


C#
# Python3 Program to implement
# the above approach
 
# Function to find the minimum
# steps required to reduce n
def downToZero(n):
   
    # Base case
    if (n <= 3):
        return n;
 
    # Return answer based on
    # parity of n
    if(n % 2 == 0):
        return 3;
    else:
        return 4;
 
# Driver Code
if __name__ == '__main__':
    n = 4;
    print(downToZero(n));
     
# This code is contributed by Rohit_ranjan


Javascript
// C# Program to implement
// the above approach
using System;
class GFG{
  
// Function to find the minimum
// steps required to reduce n
static int downToZero(int n)
{
    // Base case
    if (n <= 3)
        return n;
  
    // Return answer based on
    // parity of n
    return n % 2 == 0 ? 3 : 4;
}
  
// Driver Code
public static void Main(String[] args)
{
    int n = 4;
    Console.WriteLine(downToZero(n));
}
}
 
// This code is contributed by Rajput-Ji


输出:

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

高效的方法:想法是观察到可以用N’代替N,其中N’= min(a,b)(N = a * b)(a = 1和b!= 1)。

  • 如果N为偶数,则除以N的最小值为2 。因此,直接计算f(N)= 1 + f(2)= 3。
  • 如果N为奇数,则将N1 ,即N = N – 1 。应用与偶数相同的逻辑。因此,对于奇数,所需的最小步长为4

下面是上述方法的实现:

C++

3

Java

// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the minimum
// steps required to reduce n
int downToZero(int n)
{
    // Base case
    if (n <= 3)
        return n;
 
    // Return answer based on
    // parity of n
    return n % 2 == 0 ? 3 : 4;
}
 
// Driver Code
int main()
{
    int n = 4;
    cout << downToZero(n);
 
    return 0;
}

Python3

// Java Program to implement
// the above approach
class GFG{
  
// Function to find the minimum
// steps required to reduce n
static int downToZero(int n)
{
    // Base case
    if (n <= 3)
        return n;
  
    // Return answer based on
    // parity of n
    return n % 2 == 0 ? 3 : 4;
}
  
// Driver Code
public static void main(String[] args)
{
    int n = 4;
    System.out.println(downToZero(n));
}
}
 
// This code is contributed by rock_cool

C#

# Python3 Program to implement
# the above approach
 
# Function to find the minimum
# steps required to reduce n
def downToZero(n):
   
    # Base case
    if (n <= 3):
        return n;
 
    # Return answer based on
    # parity of n
    if(n % 2 == 0):
        return 3;
    else:
        return 4;
 
# Driver Code
if __name__ == '__main__':
    n = 4;
    print(downToZero(n));
     
# This code is contributed by Rohit_ranjan

Java脚本

// C# Program to implement
// the above approach
using System;
class GFG{
  
// Function to find the minimum
// steps required to reduce n
static int downToZero(int n)
{
    // Base case
    if (n <= 3)
        return n;
  
    // Return answer based on
    // parity of n
    return n % 2 == 0 ? 3 : 4;
}
  
// Driver Code
public static void Main(String[] args)
{
    int n = 4;
    Console.WriteLine(downToZero(n));
}
}
 
// This code is contributed by Rajput-Ji
输出:

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