class Solution {//3ms
public int nearestValidPoint(int x, int y, int[][] points) {
int idx = 0;
int min_md = Integer.MAX_VALUE;;
for(int[] point:points){
if(x==point[0]||y==point[1]){//valid point
min_md = Math.min(min_md,Math.abs(x-point[0])+Math.abs(y-point[1]));
}
}
if(min_md == Integer.MAX_VALUE)
return -1;
for(int point[]:points){
if(x==point[0]||y==point[1]){
int md = Math.abs(x-point[0])+Math.abs(y-point[1]);
if(min_md==md)
return idx;
}
idx++;
}
throw null;
}
}