📌  相关文章
📜  通过给定操作将 N 减少到 0 的最小步骤

📅  最后修改于: 2021-09-22 10:34:37             🧑  作者: Mango

给定一个整数N ,任务是通过以下操作之一找到将N减少到0的最小移动次数:

  • 将 N 减少 1。
  • 如果 N 可被 2 整除,则将 N 减少到 (N/2)。
  • 如果 N 可被 3 整除,则将 N 减少到 (N/3)。

例子:

朴素的方法:这个想法是对所有可能的移动使用递归。以下是步骤:

  1. 观察问题的基本情况,如果N < 2那么对于所有情况,答案将是 N 本身。
  2. 对于N 的每个值,在两种可能的情况之间进行选择:
    • 减少 n 直到 n % 2 == 0 然后用 count = 1 + n%2 + f(n/2) 更新 n /= 2
    • 减少 n 直到 n % 3 == 0 然后用 count = 1 + n%3 + f(n/3) 更新 n /= 3
  3. 计算结果的递推关系为:

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum
// number to steps to reduce N to 0
int minDays(int n)
{
     
    // Base case
    if (n < 1)
        return n;
         
    // Recursive call to count the
    // minimum steps needed
    int cnt = 1 + min(n % 2 + minDays(n / 2),
                      n % 3 + minDays(n / 3));
 
    // Return the answer
    return cnt;
}
 
// Driver Code
int main()
{
     
    // Given number N
    int N = 6;
     
    // Function call
    cout << minDays(N);
     
    return 0;
}
 
// This code is contributed by 29AjayKumar


Java
// Java program for the above approach
class GFG{
   
    // Function to find the minimum
    // number to steps to reduce N to 0
    static int minDays(int n)
    {
 
        // Base case
        if (n < 1)
            return n;
 
        // Recursive Call to count the
        // minimum steps needed
        int cnt = 1 + Math.min(n % 2 + minDays(n / 2),
                               n % 3 + minDays(n / 3));
 
        // Return the answer
        return cnt;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given Number N
        int N = 6;
 
        // Function Call
        System.out.print(minDays(N));
    }
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 program for the above approach
 
# Function to find the minimum
# number to steps to reduce N to 0
def minDays(n):
 
    # Base case
    if n < 1:
        return n
       
    # Recursive Call to count the
    # minimum steps needed
    cnt = 1 + min(n % 2 + minDays(n // 2),
                  n % 3 + minDays(n // 3))
 
    # Return the answer
    return cnt
 
# Driver Code
 
# Given Number N
N = 6
 
# Function Call
print(str(minDays(N)))


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to find the minimum
// number to steps to reduce N to 0
static int minDays(int n)
{   
    // Base case
    if (n < 1)
        return n;
 
    // Recursive call to count the
    // minimum steps needed
    int cnt = 1 + Math.Min(n % 2 + minDays(n / 2),
                           n % 3 + minDays(n / 3));
 
    // Return the answer
    return cnt;
}
 
// Driver Code
public static void Main(String[] args)
{   
    // Given number N
    int N = 6;
 
    // Function call
    Console.Write(minDays(N));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


C++
// C++ program for
// the above approach
#include
using namespace std;
 
// Function to find the minimum
// number to steps to reduce N to 0
int count(int n)
{
  // Dictionary for storing
  // the precomputed sum
  map dp;
 
  // Bases Cases
  dp[0] = 0;
  dp[1] = 1;
 
  // Check if n is not in dp then
  // only call the function so as
  // to reduce no of recursive calls
  if ((dp.find(n) == dp.end()))
    dp[n] = 1 + min(n % 2 +
                    count(n / 2), n % 3 +
                    count(n / 3));
 
  // Return the answer
  return dp[n];
}
 
// Driver Code
int main()
{   
  // Given number N
  int N = 6;
 
  // Function call
  cout << count(N);
}
 
// This code is contributed by gauravrajput1


Java
// Java program for the above approach
import java.util.HashMap;
 
class GFG{
     
// Function to find the minimum
// number to steps to reduce N to 0
static int count(int n)
{
     
    // Dictionary for storing
    // the precomputed sum
    HashMap dp = new HashMap();
 
    // Bases Cases
    dp.put(0, 0);
    dp.put(1, 1);
 
    // Check if n is not in dp then
    // only call the function so as
    // to reduce no of recursive calls
    if (!dp.containsKey(n))
        dp.put(n, 1 + Math.min(n % 2 +
                 count(n / 2), n % 3 +
                 count(n / 3)));
 
    // Return the answer
    return dp.get(n);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given number N
    int N = 6;
 
    // Function call
    System.out.println(String.valueOf((count(N))));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Function to find the minimum
# number to steps to reduce N to 0
def count(n):
   
    # Dictionary for storing
    # the precomputed sum
    dp = dict()
 
    # Bases Cases
    dp[0] = 0
    dp[1] = 1
 
    # Check if n is not in dp then
    # only call the function so as
    # to reduce no of recursive calls
    if n not in dp:
        dp[n] = 1 + min(n % 2 + count(n//2), n % 3 + count(n//3))
 
    # Return the answer
    return dp[n]
 
 
# Driver Code
 
# Given Number N
N = 6
 
# Function Call
print(str(count(N)))


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG{
     
// Function to find the minimum
// number to steps to reduce N to 0
static int count(int n)
{   
    // Dictionary for storing
    // the precomputed sum
    Dictionary dp = new Dictionary();
 
    // Bases Cases
    dp.Add(0, 0);
    dp.Add(1, 1);
 
    // Check if n is not in dp then
    // only call the function so as
    // to reduce no of recursive calls
    if (!dp.ContainsKey(n))
        dp.Add(n, 1 + Math.Min(n % 2 + count(n / 2),
                               n % 3 + count(n / 3)));
 
    // Return the answer
    return dp[n];
}
 
// Driver Code
public static void Main(String[] args)
{   
    // Given number N
    int N = 6;
 
    // Function call
    Console.WriteLine(String.Join("",
                                  (count(N))));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
4

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

高效的方法:这个想法是使用动态规划。由于重复子问题的数量,上述递归方法导致 TLE。使用字典优化上述方法,以跟踪已执行递归调用的值,以减少进一步的计算,从而可以更快地访问值。

下面是上述方法的实现:

C++

// C++ program for
// the above approach
#include
using namespace std;
 
// Function to find the minimum
// number to steps to reduce N to 0
int count(int n)
{
  // Dictionary for storing
  // the precomputed sum
  map dp;
 
  // Bases Cases
  dp[0] = 0;
  dp[1] = 1;
 
  // Check if n is not in dp then
  // only call the function so as
  // to reduce no of recursive calls
  if ((dp.find(n) == dp.end()))
    dp[n] = 1 + min(n % 2 +
                    count(n / 2), n % 3 +
                    count(n / 3));
 
  // Return the answer
  return dp[n];
}
 
// Driver Code
int main()
{   
  // Given number N
  int N = 6;
 
  // Function call
  cout << count(N);
}
 
// This code is contributed by gauravrajput1

Java

// Java program for the above approach
import java.util.HashMap;
 
class GFG{
     
// Function to find the minimum
// number to steps to reduce N to 0
static int count(int n)
{
     
    // Dictionary for storing
    // the precomputed sum
    HashMap dp = new HashMap();
 
    // Bases Cases
    dp.put(0, 0);
    dp.put(1, 1);
 
    // Check if n is not in dp then
    // only call the function so as
    // to reduce no of recursive calls
    if (!dp.containsKey(n))
        dp.put(n, 1 + Math.min(n % 2 +
                 count(n / 2), n % 3 +
                 count(n / 3)));
 
    // Return the answer
    return dp.get(n);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given number N
    int N = 6;
 
    // Function call
    System.out.println(String.valueOf((count(N))));
}
}
 
// This code is contributed by Amit Katiyar

蟒蛇3

# Python3 program for the above approach
 
# Function to find the minimum
# number to steps to reduce N to 0
def count(n):
   
    # Dictionary for storing
    # the precomputed sum
    dp = dict()
 
    # Bases Cases
    dp[0] = 0
    dp[1] = 1
 
    # Check if n is not in dp then
    # only call the function so as
    # to reduce no of recursive calls
    if n not in dp:
        dp[n] = 1 + min(n % 2 + count(n//2), n % 3 + count(n//3))
 
    # Return the answer
    return dp[n]
 
 
# Driver Code
 
# Given Number N
N = 6
 
# Function Call
print(str(count(N)))

C#

// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG{
     
// Function to find the minimum
// number to steps to reduce N to 0
static int count(int n)
{   
    // Dictionary for storing
    // the precomputed sum
    Dictionary dp = new Dictionary();
 
    // Bases Cases
    dp.Add(0, 0);
    dp.Add(1, 1);
 
    // Check if n is not in dp then
    // only call the function so as
    // to reduce no of recursive calls
    if (!dp.ContainsKey(n))
        dp.Add(n, 1 + Math.Min(n % 2 + count(n / 2),
                               n % 3 + count(n / 3)));
 
    // Return the answer
    return dp[n];
}
 
// Driver Code
public static void Main(String[] args)
{   
    // Given number N
    int N = 6;
 
    // Function call
    Console.WriteLine(String.Join("",
                                  (count(N))));
}
}
 
// This code is contributed by Rajput-Ji

Javascript


输出:
3

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