Problem · Math
Particle Moves to Target
Learn this problemProblem statement
A particle starts at integer coordinate (startX, startY) and must reach (targetX, targetY) on an unbounded grid. You may use these operations any nonnegative number of times:
- PROTON: move one cell right, from
(x, y)to(x + 1, y). - NEUTRON: move one cell down, from
(x, y)to(x, y - 1). - ALPHA: move two cells up and two cells left, from
(x, y)to(x - 2, y + 2).
Return a three-element integer array [protons, neutrons, alphas] whose operations reach the target using the minimum possible total number of operations. The result is ordered as PROTON, NEUTRON, ALPHA.
Function
particleMoves(startX: int, startY: int, targetX: int, targetY: int) → int[]Examples
Example 1
startX = 0startY = 0targetX = 3targetY = -2return = [3,2,0]Three PROTON operations add 3 to x, and two NEUTRON operations subtract 2 from y. No ALPHA operation is needed.
Example 2
startX = 4startY = -3targetX = -2targetY = 5return = [2,0,4]Four ALPHA operations change the position by (-8, 8), and two PROTON operations restore x by 2. The net change is (-6, 8).
Example 3
startX = -5startY = 7targetX = -8targetY = 0return = [1,11,2]Two ALPHA operations contribute (-4, 4). One PROTON and eleven NEUTRON operations then produce the required net change (-3, -7).
Constraints
-10^6 <= startX, startY, targetX, targetY <= 10^6.- Movement occurs on an unbounded integer grid.
- A minimum-operation solution always exists and every returned count fits a signed 32-bit integer.