给定范围为[-INFINITY,+ INFINITY]的无穷大行和整数N ,任务是通过向前移动i或向后移动1步,找到从0开始达到N所需的最小移动次数。在我的第一个举动中。
范例:
Input: N = 18
Output: 6
Explanation:
To reach to the given value of N, perform the operations in the fullowing sequence: 1 – 1 + 3 + 4 + 5 + 6 = 18
Therefore, a total of 6 operations are required.
Input: N = 3
Output: 2
Explanation:
To reach to the given value of N, perform the operations in the fullowing sequence: 1 + 2 = 3
Therefore, a total of 2 operations are required.
方法:最初的想法是继续添加1,2,3。 。 。 。 K ,直到大于或等于所需值N为止。然后,计算从当前总和中减去所需的数字。执行以下步骤解决该问题:
- 最初,增加K直到N大于当前值。现在,停在某个位置
pos = 1 + 2 +…………。 +步数=步数*(步数+ 1)/ 2≥N.
注意: 0≤pos – N <步长。否则,最后一步是不可能的。- 情况1:如果pos = N,则“ steps”是必需的答案。
- 情况2:如果pos≠N ,则用-1替换K的任何迭代。
- 通过用-1替换任何K ,修改后的pos = pos –(K + 1)的值。由于K∈[1,步长] ,因此pos∈[pos –步长– 1,pos – 2] 。
- 显然, pos – step
。如果N ,则选择相应的K = pos – N – 1并将K替换为-1并直接到达点N。 - 如果N + 1 = pos ,则只需要执行一次-1运算。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum steps
// required to reach N by either moving
// i steps forward or 1 steps backward
int minimumsteps(int N)
{
// Stores the required count
int steps = 0;
// IF total moves required
// is less than double of N
while (steps * (steps + 1) < 2 * N) {
// Update steps
steps++;
}
// Steps required to reach N
if (steps * (steps + 1) / 2 == N + 1) {
// Update steps
steps++;
}
cout << steps;
}
// Driver Code
int main()
{
// Given value of N
int N = 18;
minimumsteps(N);
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the minimum steps
// required to reach N by either moving
// i steps forward or 1 steps backward
static void minimumsteps(int N)
{
// Stores the required count
int steps = 0;
// IF total moves required
// is less than double of N
while (steps * (steps + 1) < 2 * N)
{
// Update steps
steps++;
}
// Steps required to reach N
if (steps * (steps + 1) / 2 == N + 1)
{
// Update steps
steps++;
}
System.out.println(steps);
}
// Driver code
public static void main(String[] args)
{
// Given value of N
int N = 18;
minimumsteps(N);
}
}
// This code is contributed by code_hunt.
Python3
# Function to find the minimum steps
# required to reach N by either moving
# i steps forward or 1 steps backward
def minimumsteps(N) :
# Stores the required count
steps = 0
# IF total moves required
# is less than double of N
while (steps * (steps + 1) < 2 * N) :
# Update steps
steps += 1
# Steps required to reach N
if (steps * (steps + 1) / 2 == N + 1) :
# Update steps
steps += 1
print(steps)
# Driver code
N = 18;
minimumsteps(N)
# This code is contributed by Dharanendra L V
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the minimum steps
// required to reach N by either moving
// i steps forward or 1 steps backward
static void minimumsteps(int N)
{
// Stores the required count
int steps = 0;
// IF total moves required
// is less than double of N
while (steps * (steps + 1) < 2 * N) {
// Update steps
steps++;
}
// Steps required to reach N
if (steps * (steps + 1) / 2 == N + 1) {
// Update steps
steps++;
}
Console.WriteLine(steps);
}
// Driver code
static public void Main()
{
// Given value of N
int N = 18;
minimumsteps(N);
}
}
// This code is contributed by Dharanendra LV
Javascript
输出:
6
时间完成度: O(sqrt(N))
辅助空间: O(1)