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

1
class Solution {
2
public:
3
int nearestValidPoint(int x, int y, vector<vector<int>> &points) {
4
int pos = -1;
5
int ans = INT_MAX;
6

7
for (int i = 0; i < points.size(); i++) {
8
if (points[i][0] == x or points[i][1] == y) {
9
int dist = abs(x - points[i][0]) + abs(y - points[i][1]);
10
if (dist < ans) {
11
pos = i;
12
ans = dist;
13
}
14
}
15
}
16
return pos;
17
}
18
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0