[Ruby] O(n) solution
Code\n\n# @param {Integer} x\n# @param {Integer} y\n# @param {Integer[][]} points\n# @return {Integer}\ndef nearest_valid_point(x, y, points)\n  res = -1\n  min
# Code\n```\n# @param {Integer} x\n# @param {Integer} y\n# @param {Integer[][]} points\n# @return {Integer}\ndef nearest_valid_point(x, y, points)\n  res = -1\n  min_md = 9999999\n\n  points.each_with_index do |point, index|\n    next unless x == point[0] || y == point[1]\n    next unless min_md > (x - point[0]).abs + (y - point[1]).abs\n    min_md = (x - point[0]).abs + (y - point[1]).abs\n    res = index\n  end\n\n  res\nend\n```
['Ruby']