class Solution:
def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:
"""
1. find valid points (share same x or y )
2. within valid points, smallest distance
3. res to record smallest index when first appear
tc O(N) sc O(1)
"""
min_d = float('inf')
res = -1
for idx, (i,j) in enumerate(points):
#dx,dy = x-i,y-j
if i == x or j == y: # if dx*dy == 0 and abs(dx+dy) < min_d
d = abs(i-x) + abs(j-y)
if min_d > d:
res = idx
min_d = d
return res