Tap here to switch tabs
Problem
Submissions

Problem: Find Nearest Point That Has the Same X or Y Coordinate

med
30 min
Try to solve the Find Nearest Point That Has the Same X or Y Coordinate problem.

Statement

You are given two integers x and y, which represent your current location (x, y) on a 2extD2 ext{D} coordinate grid. You are also given an array points where each points[i] =[ai,bi]= [a_i, b_i] denotes a point on the same grid.

A point is valid if it shares either the same x coordinate or the same y coordinate as your current location (that is, ai=a_i = x or bi=b_i = y).

Return the 00 based index of the valid point that has the smallest Manhattan distance to your current location. If multiple valid points share the same minimum Manhattan distance, return the one with the smallest index. If no valid point exists, return −1-1.

The Manhattan distance between two coordinates (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) is defined as ∣x1−x2∣+∣y1−y2∣|x_1 - x_2| + |y_1 - y_2|.

Constraints:

  • 1≤1 \leq points.length ≤104\leq 10^4

  • points[i].length =2= 2

  • 1≤1 \leq x, y, a_i, b_i ≤104\leq 10^4

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Find Nearest Point That Has the Same X or Y Coordinate

med
30 min
Try to solve the Find Nearest Point That Has the Same X or Y Coordinate problem.

Statement

You are given two integers x and y, which represent your current location (x, y) on a 2extD2 ext{D} coordinate grid. You are also given an array points where each points[i] =[ai,bi]= [a_i, b_i] denotes a point on the same grid.

A point is valid if it shares either the same x coordinate or the same y coordinate as your current location (that is, ai=a_i = x or bi=b_i = y).

Return the 00 based index of the valid point that has the smallest Manhattan distance to your current location. If multiple valid points share the same minimum Manhattan distance, return the one with the smallest index. If no valid point exists, return −1-1.

The Manhattan distance between two coordinates (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) is defined as ∣x1−x2∣+∣y1−y2∣|x_1 - x_2| + |y_1 - y_2|.

Constraints:

  • 1≤1 \leq points.length ≤104\leq 10^4

  • points[i].length =2= 2

  • 1≤1 \leq x, y, a_i, b_i ≤104\leq 10^4