#1779
Find Nearest Point That Has the Same X or Y Coordinate
pupil · 305 · lc easy +21 · verified · 69.9% accepted · 880 likes · top 78%
Description
You are at (x, y). A point (ai, bi) is valid if ai == x or bi == y. Return the 0-indexed position of the valid point with the smallest Manhattan distance from you, breaking ties by smallest index. Return -1 if no valid points exist.
Example 1:
Input: x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]]
Output: 2
Explanation: Of all the points, only [3,1], [2,4] and [4,4] are valid. Of the valid points, [2,4] and [4,4] have the smallest Manhattan distance from your current location, with a distance of 1. [2,4] has the smallest index, so return 2.
Example 2:
Input: x = 3, y = 4, points = [[3,4]]
Output: 0
Explanation: The answer is allowed to be on the same location as your current location.
Example 3:
Input: x = 3, y = 4, points = [[2,3]]
Output: -1
Explanation: There are no valid points.
Code
1
2
3