I compute the absolute distances from each moving person to Person 3: dx = |x - z| and dy = |y - z|. Since both move at the same speed, the one with the smaller distance arrives first. If dx < dy, I return 1; if dy < dx, I return 2; otherwise, they arrive simultaneously and I return 0.
class Solution:
def findClosest(self, x: int, y: int, z: int) -> int:
dx = abs(x - z)
dy = abs(y - z)
if dx < dy:
return 1
if dy < dx:
return 2
return 0
- Time: O(1)
- Space: O(1)
