📜  在无限线上达到目标的最小移动量|套装2

📅  最后修改于: 2021-04-29 06:43:49             🧑  作者: Mango

给定无限线上的目标位置(从-无限到+无限)。从0表格开始,您必须按照以下说明进行移动才能到达目标:在第ii步中,您可以向前或向后执行i步。找到达到目标所需的最小移动量。

例子 :

Input : target = 3
Output : 2
Explanation:
On the first move we step from 0 to 1.
On the second step we step from 1 to 3.

Input: target = 2
Output: 3
Explanation:
On the first move we step from 0 to 1.
On the second move we step  from 1 to -1.
On the third move we step from -1 to 2.

方法 :

这个想法类似于此处的O(n)方法中讨论的。
继续添加总和= 1 + 2 + .. + n> =目标。求解该二次方程式可得出最小的n,使得总和> =目标,即求解n(n + 1)/ 2中的n –目标> = 0可获得最小的n。
如果sum ==目标,则答案为n。现在是下一个总和大于目标的情况。通过索引比目标(即和目标)领先多少步来找到差异。

情况1:差值等于n的答案是(因为总是会发生移动翻转,这将导致目标移动)。
情况2:差是奇数,然后再走一步,即将n + 1加到和,然后再次取差。如果差异是n + 1,则答案又是另一步,这肯定会造成差异,即使答案是n + 2。

说明:由于差异是奇数。目标是奇数或偶数。

情况1:n为偶数(1 + 2 + 3 +…+ n),然后加n + 1会使差为偶数。
情况2:n为奇数,然后加n + 1不会有什么区别,即使如此,又再走一步,即n + 2。

C++
// CPP code to find minimum moves
// to reach target
#include 
using namespace std;
 
// Function to find minimum steps
// to reach target
int StepstoReachTarget(int target)
{
    // Handling negatives
    // by symmetry
    target = abs(target);
 
    // Keep moving while sum is
    // smaller i.e calculating n
    int n = ceil((-1.0 + sqrt(1 + 8.0 * target)) / 2);
    int sum = n * (n + 1) / 2;
 
    if (sum == target)
        return n;
 
    int d = sum - target;
 
    // case 1 : d is even
    if ((d & 1) == 0)
        return n;
 
    // d is odd
    else
        return n + ((n & 1) ? 2 : 1);
}
 
// Driver code
int main()
{
    int target = 5;
   
    // Function call
    cout << StepstoReachTarget(target);
    return 0;
}


Java
// Java code to find minimum moves
// to reach target
import java.lang.*;
 
class GFG {
 
    // Function to find minimum steps
    // to reach target
    static int StepstoReachTarget(int target)
    {
 
        // Handling negatives
        // by symmetry
        target = Math.abs(target);
 
        // Keep moving while sum is
        // smaller i.e calculating n
        int n = (int)Math.ceil(
            (-1.0 + (int)Math.sqrt(1 + 8.0 * target)) / 2);
 
        int sum = n * (n + 1) / 2;
 
        if (sum == target)
            return n;
 
        int d = sum - target;
 
        // case 1 : d is even
        if ((d & 1) == 0)
            return n;
 
        // d is odd
        else
            return n + ((n & 1) != 0 ? 2 : 1);
    }
 
    // Driver code
    public static void main(String[] arg)
    {
        int target = 5;
       
        // Function call
        System.out.println(StepstoReachTarget(target));
    }
}
 
// This code is contributed by
// Smitha Dinesh Semwal


Python3
# Python code to find minimum
# moves to reach target
import math
 
# Function to find minimum
# steps to reach target
 
 
def StepstoReachTarget(target):
 
    # Handling negatives
    # by symmetry
    target = abs(target)
 
    # Keep moving while sum is
    # smaller i.e calculating n
    n = math.ceil((-1.0 + math.sqrt(1 +
                                    8.0 * target)) / 2)
    sum = n * (n + 1) / 2
 
    if (sum == target):
        return n
 
    d = sum - target
 
    # case 1 : d is even
    if ((int(d) & 1) == 0):
        return n
 
    # d is odd
    else:
        if(int(d) & 1):
            return n + 2
        return n + 1
 
 
# Driver code
target = 5
 
# Function call
print(StepstoReachTarget(target))
 
# This code is contributed by
# Manish Shaw(manishshaw1)


C#
// C# code to find minimum moves
// to reach target
using System;
 
class GFG {
 
    // Function to find minimum steps
    // to reach target
    static int StepstoReachTarget(int target)
    {
 
        // Handling negatives
        // by symmetry
        target = Math.Abs(target);
 
        // Keep moving while sum is
        // smaller i.e calculating n
        int n = (int)Math.Ceiling(
            (-1.0 + (int)Math.Sqrt(1 + 8.0 * target)) / 2);
 
        int sum = n * (n + 1) / 2;
 
        if (sum == target)
            return n;
 
        int d = sum - target;
 
        // case 1 : d is even
        if ((d & 1) == 0)
            return n;
 
        // d is odd
        else
            return n + ((n & 1) != 0 ? 2 : 1);
    }
 
    // Driver code
    public static void Main()
    {
        int target = 5;
       
        // Function call
        Console.Write(StepstoReachTarget(target));
    }
}
 
// This code is contributed by nitin mittal.


PHP


Javascript


输出 :
5