LeetCode solutions

1888. Find Nearest Point That Has the Same X or Y Coordinate

My accepted Python solution to LeetCode problem 1888, Find Nearest Point That Has the Same X or Y Coordinate, running in 11ms.

  • Difficulty: Easy
  • Python
  • Runtime 11ms
  • Memory 21.2MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 11ms, memory 21.2MB, accepted 2025-12-30.

python
class Solution:
    def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:
        min_dist = float('inf')
        result = -1
        for i, (px, py) in enumerate(points):
            if px == x or py == y:
                dist = abs(px - x) + abs(py - y)
                if dist < min_dist:
                    min_dist = dist
                    result = i
        return result

Source