📌  相关文章
📜  最小化达到值 N 所需的步骤

📅  最后修改于: 2021-10-25 06:48:35             🧑  作者: Mango

给定范围[-INFINITY, +INFINITY] 中的无限数和整数N ,任务是找到到达N所需的最小移动次数,从0开始,通过向前移动i步或向后移动1步在i动作中。

例子:

方法:这个想法是最初,不断添加1, 2, 3 。 . . . K ,直到它大于或等于所需的值N 。然后,计算要从当前总和中减去的所需数字。请按照以下步骤解决问题:

  • 最初,递增K直到N大于当前值。现在,停在某个位置
    位置 = 1 + 2 + …………。 + 步数 = 步数 *(步数 + 1)/ 2 ≥ N。
    注意: 0 ≤ pos – N < steps 。否则,最后一步是不可能的。
    • 情况 1:如果pos = N,则 ‘steps’ 是必需的答案。
    • 情况 2:如果pos ≠ N ,则将K 的任何迭代替换为 -1。
  • 通过将任何K替换为-1pos = pos – (K + 1)的修改值。由于K ∈ [1, steps] ,则pos ∈ [pos – steps – 1, pos – 2]
  • 显然pos – step < N 。如果N < pos – 1 ,则选择对应的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)