1779. Find Nearest Point That Has the Same X or Y Coordinate
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 69% Topics: Array
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
class Solution(object):
def nearestValidPoint(self, x, y, points):
"""
:type x: int
:type y: int
:type points: List[List[int]]
:rtype: int
"""
smallest, idx = float("inf"), -1
for i, (r, c) in enumerate(points):
dx, dy = x-r, y-c
if dx*dy == 0 and abs(dx)+abs(dy) < smallest:
smallest = abs(dx)+abs(dy)
idx = i
return idx
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions