class Solution {
public boolean isValid(int x, int y, int[] point) {
return x == point[0] || y == point[1];
}
public int manhattan(int x, int y, int[] point) {
return Math.abs(x-point[0]) + Math.abs(y-point[1]);
}
public int nearestValidPoint(int x, int y, int[][] points) {
int distance = Integer.MAX_VALUE, ans = -1;
for (int i = 0; i < points.length; i++) {
if (isValid(x, y, points[i]) == false) continue;
if (manhattan(x, y, points[i]) < distance) { distance = manhattan(x, y, points[i]); ans = i; }
}
return ans;
}
}