#2543
Check if Point Is Reachable
candidate master · 1585 · lc hard +32 · verified · 44.8% accepted · 262 likes · top 27%
Description
Starting from (1, 1) on an infinite grid, each move transforms your position (x, y) to one of: (x, y - x), (x - y, y), (2 * x, y), or (x, 2 * y). Given integers targetX and targetY, return true if (targetX, targetY) is reachable in any number of moves, and false otherwise.
Example 1:
Input: targetX = 6, targetY = 9
Output: false
Explanation: It is impossible to reach (6,9) from (1,1) using any sequence of moves, so false is returned.
Example 2:
Input: targetX = 4, targetY = 7
Output: true
Explanation: You can follow the path (1,1) -> (1,2) -> (1,4) -> (1,8) -> (1,7) -> (2,7) -> (4,7).
Code
1
2
3