Java || Easy to Understand || 1ms || beats 100% || O(points.length)
\n    // O(points.length) O(1)\n\tpublic int nearestValidPoint(int x, int y, int[][] points) {\n\n\t\tint min = Integer.MAX_VALUE, ans = -1, idx = -1;\n\t\tfor 
\n    // O(points.length) O(1)\n\tpublic int nearestValidPoint(int x, int y, int[][] points) {\n\n\t\tint min = Integer.MAX_VALUE, ans = -1, idx = -1;\n\t\tfor (int[] point : points) {\n\t\t\tidx++;\n\t\t\tif (point[0] == x || point[1] == y) {\n\t\t\t\tint dist = distance(x, y, point[0], point[1]);\n\t\t\t\tif (dist < min) {\n\t\t\t\t\tmin = dist;\n\t\t\t\t\tans = idx;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn ans;\n\t}\n\n\t// O(1)\n\tpublic int distance(int x1, int y1, int x2, int y2) {\n\t\treturn Math.abs(x1 - x2) + Math.abs(y1 - y2);\n\t}
[]