Find Nearest Point That Has the Same X or Y Coordinate LeetCode solution in Java

1
class Solution {
2
public int nearestValidPoint(int x, int y, int[][] points) {
3
int min = Integer.MAX_VALUE, index = -1, i;
4

5
for (i = 0; i < points.length; i++) {
6
if (x == points[i][0] || y == points[i][1]) {
7
int d = Math.abs(x - points[i][0]) + Math.abs(y - points[i][1]);
8
if (d < min) {
9
min = d;
10
index = i;
11
}
12
}
13
}
14
// if ( min== Integer.MAX_VALUE) return -1; --> no longer needed as index is initialized as -1
15
// in the declartion.
16
return index;
17
}
18
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0