← 返回 nvidia 的题目列表Reaching Points
类型:qbank
Given positive start and target coordinates, determine whether repeated transformations from (x, y) to either (x + y, y) or (x, x + y) can reach the target. The interview used LeetCode 780 without a stated modification.
Requirements
Start from a positive integer coordinate pair (sx, sy).
In one move, transform (x, y) into either (x + y, y) or (x, x + y).
Given a positive target pair (tx, ty), return whether the target is reachable after zero or more moves.
Notes
This is the canonical reaching-points problem, with no variant described. Clarify numeric bounds and expected integer width before coding.
Work backward because each forward move only increases one coordinate. While both target coordinates remain larger than their corresponding start coordinates, the larger target coordinate has a unique predecessor direction; replace it with its remainder modulo the smaller coordinate to collapse repeated subtraction in one step. If the coordinates become equal before reaching the start, no further reverse move is possible.
After the loop, accept only if one coordinate equals its start value and the remaining nonnegative difference is divisible by that fixed coordinate: tx == sx && ty >= sy && (ty - sy) % sx == 0, or the symmetric condition for ty == sy. Reject any target coordinate below its start. This runs in O(log(max(tx, ty))) time and O(1) space.
Preparation
Practice reversing monotone coordinate transformations and state the invariant that makes the reverse direction valid.
Test equality at the start, one-coordinate matches, large coordinate gaps, and targets smaller than the start.