← 返回 uber 的题目列表Minimum Knight Moves
类型:qbank
On an infinite chess board with a knight starting at [0, 0], return the minimum number of moves needed to reach a target square [x, y]. The answer is guaranteed to exist.
Minimum Knight Moves
On an infinite chess board with a knight starting at [0, 0], return the minimum number of moves needed to reach a target square [x, y]. The answer is guaranteed to exist.
SWE
bfs
graph
math
medium
Frequency
Single report
Last asked
2026-03-28
Stage
oa
Minimum Knight Moves
In an infinite chess board with coordinates from -infinity to +infinity, you have a knight at square [0, 0].
A knight can move in 8 possible ways: [+/-1, +/-2] and [+/-2, +/-1].
Return the minimum number of steps needed to move the knight to square [x, y]. It is guaranteed the answer exists.
Examples
Example 1:
Input: x = 2, y = 1
Output: 1
Example 2:
Input: x = 5, y = 5
Output: 4
Example 3:
Input: x = 0, y = 0
Output: 0
Explanation:
The knight starts at the target square.
Constraints
-300 <= x, y <= 300
0 <= |x| + |y| <= 300