1197. Minimum Knight Moves

Practice Minimum Knight Moves for free on NudgeCode: Medium, Graph pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

On an infinite chessboard a knight starts at (0, 0). A knight move goes two squares in one axis direction and one square in the other (8 possible moves). Return the minimum number of moves needed to reach (x, y). It is always possible.

Example

x = 2, y = 1  ->  1
x = 5, y = 5  ->  4      ((0,0) -> (2,1) -> (4,2) -> (3,4) -> (5,5))

Constraints