Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created September 4, 2025 21:45
Show Gist options
  • Save Ifihan/558f7348d619d349669d69284f2ad4ae to your computer and use it in GitHub Desktop.
Save Ifihan/558f7348d619d349669d69284f2ad4ae to your computer and use it in GitHub Desktop.
Find Closest Person

Question

Approach

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.

Implementation

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

Complexities

  • Time: O(1)
  • Space: O(1)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment