607. Minimum Knight Moves
A knight stands on square (0, 0) of an infinite chessboard — squares in every direction, no edges anywhere. In one move it does what chess knights do: two squares along one axis, then one square along the other, giving eight possible jumps like (+2, +1) or (-1, +2).
Your function receives the target coordinates x and y (either may be negative) and must return the minimum number of moves needed to land exactly on (x, y). A knight can reach every square of an infinite board, so an answer always exists — and if the target is (0, 0) itself, the answer is 0.
Example 1:
Input: x = 2, y = 1
Output: 1
Explanation: (2, 1) is one of the knight's eight jumps from the origin: two right, one up. One move.
Example 2:
Input: x = 5, y = 5
Output: 4
Explanation: One optimal route is (0,0) → (2,1) → (4,2) → (3,4) → (5,5): four legal L-jumps, and no shorter sequence reaches (5,5).
Constraints:
- -300 ≤ x, y ≤ 300
- The board is infinite: intermediate squares may have any coordinates, including negative ones
Hints:
Every move costs the same, so 'fewest moves' means shortest path in an unweighted graph — that is breadth-first search from (0, 0), where each square's eight jump targets are its neighbors.
The board is symmetric under reflection, so the distance to (x, y) equals the distance to (|x|, |y|). Folding the target into the first quadrant shrinks the search fourfold.
Even heading to a first-quadrant target, the optimal path may dip slightly negative — (1, 1) is reached via (2, -1). Allowing coordinates down to -2 is enough; clamping at 0 gives wrong answers near the origin.
▶ Run checks these sample cases. Submit also runs hidden edge cases.
Input: x = 2, y = 1
Expected output: 1