通过增加 1 或将值加倍最多 D 次,从 1 达到 N
给定一个整数N和一个整数D ,任务是通过加 1 或将值加倍,在最小移动中从 1 达到 N ,但加倍最多可以进行D次。
例子:
Input: N = 20, D = 4
Output: 5
Explanation: The flow can be seen as 1 -> 2 -> 4 -> 5 -> 10 -> 20
Input: N = 10, D = 0
Output: 9
方法:可以使用递归来解决该任务:
- 声明一个变量来存储最小移动作为答案
- 首先检查是否达到了目标,如果是找到存储当前移动的最小值并在ans中回答
- 然后检查加倍动作D是否已经用尽,但目标还没有达到,
- 然后通过在 current_moves 中添加 ( N-current_value ) 将剩余的移动添加为递增移动。
- 求current_moves和ans的最小值,并存入ans
- 如果current_value已超过N ,则返回
- 如果以上情况均不匹配,则递归调用该函数以执行以下两项操作:
- 加倍current_value
- current_value加 1 。
- 返回最终的ans 。
下面是上述方法的实现。
C++
// C++ code to implement the approach
#include
using namespace std;
int ans = 0;
// Utility function to find
// the minimum number of moves required
void move(int& N, int& D, long s,
int cd, int temp)
{
if (s == N) {
ans = min(ans, temp);
return;
}
if (cd == D && s <= N) {
temp += (N - s);
ans = min(ans, temp);
return;
}
if (s > N)
return;
move(N, D, s * 2, cd + 1, temp + 1);
move(N, D, s + 1, cd, temp + 1);
}
// Function to call the utility function
int minMoves(int N, int D)
{
ans = N;
move(N, D, 1, 0, 0);
return ans;
}
// Driver code
int main()
{
int N = 20, D = 4;
cout << minMoves(N, D);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static int ans = 0;
// Utility function to find
// the minimum number of moves required
static void move(int N, int D, long s, int cd, int temp)
{
if (s == N) {
ans = Math.min(ans, temp);
return;
}
if (cd == D && s <= N) {
temp += (N - s);
ans = Math.min(ans, temp);
return;
}
if (s > N)
return;
move(N, D, s * 2, cd + 1, temp + 1);
move(N, D, s + 1, cd, temp + 1);
}
// Function to call the utility function
static int minMoves(int N, int D)
{
ans = N;
move(N, D, 1, 0, 0);
return ans;
}
// Driver code
public static void main (String[] args) {
int N = 20, D = 4;
System.out.print(minMoves(N, D));
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python program for the above approach
ans = 0;
# Utility function to find
# the minimum number of moves required
def move(N, D, s, cd, temp):
global ans;
if (s == N):
ans = min(ans, temp);
return;
if (cd == D and s <= N):
temp += (N - s);
ans = min(ans, temp);
return;
if (s > N):
return;
move(N, D, s * 2, cd + 1, temp + 1);
move(N, D, s + 1, cd, temp + 1);
# Function to call the utility function
def minMoves(N, D):
global ans;
ans = N;
move(N, D, 1, 0, 0);
return ans;
# Driver code
if __name__ == '__main__':
N = 20;
D = 4;
print(minMoves(N, D));
# This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
class GFG {
static int ans = 0;
// Utility function to find
// the minimum number of moves required
static void move(int N, int D, long s, int cd, int temp)
{
if (s == N) {
ans = Math.Min(ans, temp);
return;
}
if (cd == D && s <= N) {
temp += (int)(N - s);
ans = Math.Min(ans, temp);
return;
}
if (s > N)
return;
move(N, D, s * 2, cd + 1, temp + 1);
move(N, D, s + 1, cd, temp + 1);
}
// Function to call the utility function
static int minMoves(int N, int D)
{
ans = N;
move(N, D, 1, 0, 0);
return ans;
}
// Driver code
public static void Main () {
int N = 20, D = 4;
Console.Write(minMoves(N, D));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
5
时间复杂度: O(N)
辅助空间: O(1)