Reach a Number
You are standing at position 0 on an infinite number line. There is a destination at position target.
You can make some number of moves numMoves so that:
- On each move, you can either go left or right.
- During the
i^thmove, starting fromi == 1toi == numMoves, you takeisteps in the chosen direction.
Given the integer target, return the minimum number of moves required, i.e. the minimum numMoves, to reach the destination.
Example 1
Input
target = 2Output
3On the first move you go from 0 to 1, on the second from 1 to -1, and on the third from -1 to 2.
Example 2
Input
target = 3Output
2On the first move you go from 0 to 1, and on the second from 1 to 3.
Constraints
- -10^9 <= target <= 10^9
- target != 0