[Ruby] O(n)
\n# @param {Integer} x\n# @param {Integer} y\n# @param {Integer[][]} points\n# @return {Integer}\ndef nearest_valid_point(x, y, points)\n  distances = []\n  \n 
```\n# @param {Integer} x\n# @param {Integer} y\n# @param {Integer[][]} points\n# @return {Integer}\ndef nearest_valid_point(x, y, points)\n  distances = []\n  \n  points.each_with_index do |point, index|\n    a, b = point\n    distances << [(x - a).abs + (y - b).abs, index] if a == x || b == y\n  end\n  \n  return -1 if distances.empty?\n  \n  distances.sort.first.last\nend\n```
['Ruby']