1197. Minimum Knight Moves

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.

 

Example 1:

Input:

 x = 2, y = 1
Output:

 1
Explanation: 

[0, 0] → [2, 1]

Example 2:

Input:

 x = 5, y = 5
Output:

 4
Explanation: 

[0, 0] → [2, 1] → [4, 2] → [3, 4] → [5, 5]

 

Constraints:

1、题目描述

一个坐标可以从 -infinity 延伸到 +infinity无限大的 棋盘上,你的 骑士 驻扎在坐标为 [0, 0] 的方格里。

骑士的走法和中国象棋中的马相似,走 “日” 字:即先向左(或右)走 1 格,再向上(或下)走 2 格;或先向左(或右)走 2 格,再向上(或下)走 1 格。

每次移动,他都可以按图示八个方向之一前进。

img

现在,骑士需要前去征服坐标为 [x, y] 的部落,请你为他规划路线。

最后返回所需的最小移动次数即可。本题确保答案是一定存在的。

示例 1:

输入:x = 2, y = 1
输出:1
解释:[0, 0] → [2, 1]

示例 2:

输入:x = 5, y = 5
输出:4
解释:[0, 0] → [2, 1] → [4, 2] → [3, 4] → [5, 5]

提示:

Difficulty:

Medium

Lock:

Prime

Company:

Amazon Facebook Google Oracle

Solution(Chinese):

LEETCODE 1197. Minimum Knight Moves 解题思路分析