File: find-nearest-point-that-has-the-same-x-or-y-coordinate/solution.py

Date: 2026-06-06

Time: 16:39

Purpose

This file solves LeetCode 1779: Find Nearest Point That Has the Same X or Y Coordinate. It implements a single function that finds the index of the closest "valid" point — one sharing an x- or y-coordinate with a reference location — using Manhattan distance as the metric.

Key Components

nearestValidPoint(x, y, points) -> int

Contract: Given a reference point (x, y) and a list of coordinate pairs, return the index of the nearest point that shares either x or y with the reference. If no valid point exists, return -1. On ties, the smallest index wins (guaranteed by the strict < comparison — first occurrence is kept).

Parameters:

Return: index into points, or -1

Patterns

Linear scan with running minimum — the classic pattern for finding an extremum in a single pass. No sorting, no heap, no auxiliary data structure. The sentinel float('inf') for min_dist avoids a special case for the first valid point.

Filter-then-reduce fused into one loop — the validity check (ai == x or bi == y) and the distance comparison happen in the same iteration, avoiding a second pass or intermediate list.

Dependencies

Imports: None. Pure function with no standard-library or third-party dependencies.

Imported by: The "Imported By" list in the prompt is misleading — those are test files from *other* problems that happen to share a common test harness structure. The actual consumer is find-nearest-point-that-has-the-same-x-or-y-coordinate/test_solution.py.

Flow

1. Initialize mindist = inf, minidx = -1.

2. Iterate over points with index via enumerate, destructuring each point as (ai, bi).

3. Validity gate: skip any point where neither coordinate matches.

4. Distance: compute Manhattan distance |x - ai| + |y - bi|.

5. Update: if this distance is strictly less than the current minimum, record it.

6. Return min_idx (still -1 if no valid point was found).

Invariants

Error Handling

None. The function trusts its inputs — no type checks, no bounds validation. If points is empty or contains no valid points, the sentinel -1 is returned. This is correct per the LeetCode contract.

Complexity

Topics to Explore

Beliefs