Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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:
(x + m, y), or(x, y + m).Return the minimum number of moves required to reach (tx, ty). If it is impossible to reach the target, return -1.
Example 1:
Input: sx = 1, sy = 2, tx = 5, ty = 4
Output: 2
Explanation:
The optimal path is:
max(1, 2) = 2. Increase the y-coordinate by 2, moving from (1, 2) to (1, 2 + 2) = (1, 4).max(1, 4) = 4. Increase the x-coordinate by 4, moving from (1, 4) to (1 + 4, 4) = (5, 4).Thus, the minimum number of moves to reach (5, 4) is 2.
Example 2:
Input: sx = 0, sy = 1, tx = 2, ty = 3
Output: 3
Explanation:
The optimal path is:
max(0, 1) = 1. Increase the x-coordinate by 1, moving from (0, 1) to (0 + 1, 1) = (1, 1).max(1, 1) = 1. Increase the x-coordinate by 1, moving from (1, 1) to (1 + 1, 1) = (2, 1).max(2, 1) = 2. Increase the y-coordinate by 2, moving from (2, 1) to (2, 1 + 2) = (2, 3).Thus, the minimum number of moves to reach (2, 3) is 3.
Example 3:
Input: sx = 1, sy = 1, tx = 2, ty = 2
Output: -1
Explanation:
(2, 2) from (1, 1) using the allowed moves. Thus, the answer is -1.Constraints:
0 <= sx <= tx <= 1090 <= sy <= ty <= 109Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
1 2 5 4
0 1 2 3
1 1 2 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3609: Minimum Moves to Reach Target in Grid
// Auto-generated Java example from go.
class Solution {
public void exampleSolution() {
}
}
// Reference (go):
// // Accepted solution for LeetCode #3609: Minimum Moves to Reach Target in Grid
// package main
//
// // https://space.bilibili.com/206214
// func minMoves(sx, sy, x, y int) (ans int) {
// for ; x != sx || y != sy; ans++ {
// if x < sx || y < sy {
// return -1
// }
// if x == y {
// if sy > 0 {
// x = 0
// } else {
// y = 0
// }
// continue
// }
// // 保证 x > y
// if x < y {
// x, y = y, x
// sx, sy = sy, sx
// }
// if x >= y*2 {
// if x%2 > 0 {
// return -1
// }
// x /= 2
// } else {
// x -= y
// }
// }
// return
// }
// Accepted solution for LeetCode #3609: Minimum Moves to Reach Target in Grid
package main
// https://space.bilibili.com/206214
func minMoves(sx, sy, x, y int) (ans int) {
for ; x != sx || y != sy; ans++ {
if x < sx || y < sy {
return -1
}
if x == y {
if sy > 0 {
x = 0
} else {
y = 0
}
continue
}
// 保证 x > y
if x < y {
x, y = y, x
sx, sy = sy, sx
}
if x >= y*2 {
if x%2 > 0 {
return -1
}
x /= 2
} else {
x -= y
}
}
return
}
# Accepted solution for LeetCode #3609: Minimum Moves to Reach Target in Grid
import math
class Solution:
def minMoves(self, sx: int, sy: int, tx: int, ty: int) -> int:
moves = 0
# Helper to check power of two
def is_power_of_two(n):
return n > 0 and (n & (n - 1)) == 0
while tx >= sx and ty >= sy:
if tx == sx and ty == sy:
return moves
if tx > ty:
# If we are in a state where y matches sy, and sx is larger than ty,
# we are purely in the doubling regime for x. We can fast-forward.
if ty == sy and sx > ty:
if tx % sx == 0 and is_power_of_two(tx // sx):
return moves + (tx // sx).bit_length() - 1
else:
return -1
if tx >= 2 * ty:
# Must have come from doubling x
if tx % 2 != 0:
return -1
tx //= 2
moves += 1
else:
# Must have come from x + y
tx -= ty
moves += 1
elif ty > tx:
# Symmetric logic for y
if tx == sx and sy > tx:
if ty % sy == 0 and is_power_of_two(ty // sy):
return moves + (ty // sy).bit_length() - 1
else:
return -1
if ty >= 2 * tx:
if ty % 2 != 0:
return -1
ty //= 2
moves += 1
else:
ty -= tx
moves += 1
else:
# tx == ty
# Can only reach equal coordinates from a state where one coordinate is 0.
# e.g. (0, y) -> (y, y) or (x, 0) -> (x, x)
res = float('inf')
# Check possibility coming from x=0
if sx == 0:
# We need to check distance from (0, sy) to (0, ty)
# This path is purely doubling y
if ty == sy:
res = min(res, moves + 1)
elif sy > 0 and ty % sy == 0 and is_power_of_two(ty // sy):
res = min(res, moves + 1 + (ty // sy).bit_length() - 1)
# If sy == 0, we can only reach (0, ty) if ty == 0, but we are at tx=ty>0 here usually
# (unless tx=ty=0 which is caught at start)
# Check possibility coming from y=0
if sy == 0:
# Check distance from (sx, 0) to (tx, 0)
if tx == sx:
res = min(res, moves + 1)
elif sx > 0 and tx % sx == 0 and is_power_of_two(tx // sx):
res = min(res, moves + 1 + (tx // sx).bit_length() - 1)
if res != float('inf'):
return res
else:
return -1
return -1
// Accepted solution for LeetCode #3609: Minimum Moves to Reach Target in Grid
// Rust example auto-generated from go reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (go):
// // Accepted solution for LeetCode #3609: Minimum Moves to Reach Target in Grid
// package main
//
// // https://space.bilibili.com/206214
// func minMoves(sx, sy, x, y int) (ans int) {
// for ; x != sx || y != sy; ans++ {
// if x < sx || y < sy {
// return -1
// }
// if x == y {
// if sy > 0 {
// x = 0
// } else {
// y = 0
// }
// continue
// }
// // 保证 x > y
// if x < y {
// x, y = y, x
// sx, sy = sy, sx
// }
// if x >= y*2 {
// if x%2 > 0 {
// return -1
// }
// x /= 2
// } else {
// x -= y
// }
// }
// return
// }
// Accepted solution for LeetCode #3609: Minimum Moves to Reach Target in Grid
// Auto-generated TypeScript example from go.
function exampleSolution(): void {
}
// Reference (go):
// // Accepted solution for LeetCode #3609: Minimum Moves to Reach Target in Grid
// package main
//
// // https://space.bilibili.com/206214
// func minMoves(sx, sy, x, y int) (ans int) {
// for ; x != sx || y != sy; ans++ {
// if x < sx || y < sy {
// return -1
// }
// if x == y {
// if sy > 0 {
// x = 0
// } else {
// y = 0
// }
// continue
// }
// // 保证 x > y
// if x < y {
// x, y = y, x
// sx, sy = sy, sx
// }
// if x >= y*2 {
// if x%2 > 0 {
// return -1
// }
// x /= 2
// } else {
// x -= y
// }
// }
// return
// }
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.