Skip to content

Commit adcb007

Browse files
solves find nearest point that has same x ort y coordinate
1 parent 9b576d6 commit adcb007

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@
433433
| 1763 | [Longest Nice Substring](https://leetcode.com/problems/longest-nice-substring) | [![Java](assets/java.png)](src/LongestNiceSubstring.java) | |
434434
| 1768 | [Merge Strings Alternately](https://leetcode.com/problems/merge-strings-alternately) | [![Java](assets/java.png)](src/MergeStringsAlternately.java) | |
435435
| 1773 | [Count Items Matching a Rule](https://leetcode.com/problems/count-items-matching-a-rule) | [![Java](assets/java.png)](src/CountItemsMatchingARule.java) | |
436-
| 1779 | [Find Nearest Point That Has the Same X or Y Coordinate](https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate) | | |
436+
| 1779 | [Find Nearest Point That Has the Same X or Y Coordinate](https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate) | [![Java](assets/java.png)](src/FindNearestPointThatHasTheSameXOrYCoordinate.java) | |
437437
| 1784 | [Check if Binary String Has at Most One Segment of Ones](https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones) | | |
438438
| 1790 | [Check if One String Swap Can Make Strings Equal](https://leetcode.com/problems/check-if-one-string-swap-can-make-strings-equal) | | |
439439
| 1791 | [Find Center of Star Graph](https://leetcode.com/problems/find-center-of-star-graph) | | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class FindNearestPointThatHasTheSameXOrYCoordinate {
2+
public int nearestValidPoint(int x, int y, int[][] points) {
3+
int minDistance = Integer.MAX_VALUE, minIndex = -1;
4+
for (int index = 0 ; index < points.length ; index++) {
5+
int[] point = points[index];
6+
if (point[0] == x || point[1] == y) {
7+
int distance = manhattanDistance(x, y, point);
8+
if (distance < minDistance) {
9+
minDistance = distance;
10+
minIndex = index;
11+
}
12+
}
13+
}
14+
return minIndex;
15+
}
16+
17+
private int manhattanDistance(int x, int y, int[] point) {
18+
return Math.abs(x - point[0]) + Math.abs(y - point[1]);
19+
}
20+
}

0 commit comments

Comments
 (0)