通过在每个步骤中添加 M/X 来最小化从 M 中获得 N 的步骤
给定一个整数N ,任务是找到从M中获得N的最小步数(最初M = 1 )。在每个步骤中,可以将M/X添加到M中,其中X是任何正整数。
例子:
Input: N = 5
Output: 3
Explanation: Initially the number is 1. 1/1 is added to make it 2.
In next step adding 2/1 = 2 it becomes 4. At last add 4/4 = 1 to get the 5.
This is the minimum steps required to convert 1 to 5
Input: N = 7
Output: 4
Explanation: Initially the number is 1.
Now 1/1 is added to it and it becomes 2.
After adding 2/1 = 2 it becomes 4. In the third 4/2 = 2 is added and it becomes 6.
At the final step add 6/6 = 1 and it becomes 7.
方法:这个问题的方法是使用动态编程。对于每个整数,有许多可能的移动。存储到达dp[]数组中每个数字所需的最小步骤,并将其用于下一个数字。请按照以下步骤操作。
- 开始从2 迭代到 N 。
- 对于每个数字i ,请执行以下操作:
- 检查从哪些数字(小于i ) ,我们可以到达i 。
- 现在对于这些数字,使用关系dp[i] = min(dp[i], dp[j]+1)找到到达i所需的最小步骤,其中j是可以到达i的数字。
- 将该最小值存储在dp[i]数组中。
- 在对所有元素进行迭代后,最多为 N 返回 dp[N] 的值。
下面是上述方法的实现:
C++
// C++ code to implement above approach
#include
using namespace std;
// Function to find the minimum steps required
int minSteps(int N)
{
vector dp(N + 1, INT_MAX);
dp[1] = 0;
// Loop to find the minimum steps to
// reach N from 1
for (int i = 2; i <= N; ++i) {
for (int j = 1; j <= i; ++j) {
// Finding the distance
// between two numbers
int distance = i - j;
if (distance == 0) {
continue;
}
// Divide the number
int divide = j / distance;
if (divide != 0) {
// Checking if the number
// can be reached or not
if (j / divide == distance) {
dp[i] = min(dp[j] + 1, dp[i]);
}
}
}
}
return dp[N];
}
// Driver code
int main()
{
int N = 7;
int ans = minSteps(N);
cout << (ans);
return 0;
}
// This code is contributed by rakeshsahni
Java
// Java code to implement above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to find the minimum steps required
static int minSteps(int N)
{
int dp[] = new int[N + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[1] = 0;
// Loop to find the minimum steps to
// reach N from 1
for (int i = 2; i <= N; ++i) {
for (int j = 1; j <= i; ++j) {
// Finding the distance
// between two numbers
int distance = i - j;
if (distance == 0) {
continue;
}
// Divide the number
int divide = j / distance;
if (divide != 0) {
// Checking if the number
// can be reached or not
if (j / divide == distance) {
dp[i]
= Math.min(dp[j] + 1,
dp[i]);
}
}
}
}
return dp[N];
}
// Driver code
public static void main(String[] args)
{
int N = 7;
int ans = minSteps(N);
System.out.println(ans);
}
}
Python
# Python] code to implement above approach
import sys
# Function to find the minimum steps required
def minSteps(N):
dp = []
dp = [sys.maxsize for i in range(N + 1)]
dp[1] = 0;
# Loop to find the minimum steps to
# reach N from 1
for i in range(2, N + 1):
for j in range(1, i + 1):
# Finding the distance
# between two numbers
distance = i - j
if (distance == 0):
continue
# Divide the number
divide = j // distance;
if (divide != 0):
# Checking if the number
# can be reached or not
if (j // divide == distance):
dp[i] = min(dp[j] + 1, dp[i])
return dp[N]
# Driver code
N = 7
ans = minSteps(N);
print(ans)
# This code is contributed by Samim Hossain Mondal.
C#
// C# program for the above approach
using System;
public class GFG{
// Function to find the minimum steps required
static int minSteps(int N)
{
int[] dp = new int[N + 1];
for(int i = 0; i < N + 1; i++)
dp[i] = Int32.MaxValue;
dp[1] = 0;
// Loop to find the minimum steps to
// reach N from 1
for (int i = 2; i <= N; ++i) {
for (int j = 1; j <= i; ++j) {
// Finding the distance
// between two numbers
int distance = i - j;
if (distance == 0) {
continue;
}
// Divide the number
int divide = j / distance;
if (divide != 0) {
// Checking if the number
// can be reached or not
if (j / divide == distance) {
dp[i]
= Math.Min(dp[j] + 1,
dp[i]);
}
}
}
}
return dp[N];
}
// Driver code
static public void Main (){
int N = 7;
int ans = minSteps(N);
Console.Write(ans);
}
}
// This code is contributed by hrithikgarg03188.
Javascript
4
时间复杂度: O(N*N)
辅助空间: O(1)