#3609
Minimum Moves to Reach Target in Grid
hard · 14.9% accepted · 55 likes · top 0%
Description
You are given four integers sx, sy, tx, and ty, representing two points (sx, sy) and (tx, ty) on an infinitely large 2D grid.
You start at (sx, sy).
At any point (x, y), define m = max(x, y). You can either:
- Move to (x + m, y), or
- Move to (x, y + m).
Return the minimum number of moves required to reach (tx, ty). If it is impossible to reach the target, return -1.
Solution