#3609
Minimum Moves to Reach Target in Grid
grandmaster · 2290 · lc hard +32 · 14.9% accepted · 55 likes · top 0%
Description
Starting at (sx, sy) on an infinite 2D grid, at each step from position (x, y) let m = max(x, y) and move to either (x + m, y) or (x, y + m).
Return the minimum number of moves to reach (tx, ty), or -1 if the target is unreachable.
Code
1
2
3