Skip to main content
LeetCode 1197, Medium. Topics: Breadth-First Search. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 19 parametrized pytest cases, and a playground notebook:

Problem

In an infinite chess board with coordinates from -infinity to +infinity, you have a knight at square [0, 0]. A knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction. Return the minimum number of steps needed to move the knight to the square [x, y]. It is guaranteed the answer exists.

Examples

Example 1

Constraints

  • -300 <= x, y <= 300
  • 0 <= |x| + |y| <= 300

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Grind, NeetCode All.
Last modified on August 25, 2026