Skip to content

Commit 7429db9

Browse files
committed
nearestValidPoint
1 parent 68c7aba commit 7429db9

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Step 2. Add the dependency
4545

4646
<summary>展开查看</summary>
4747

48+
https://leetcode.cn/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/
49+
4850
https://leetcode.cn/problems/leaf-similar-trees
4951

5052
https://leetcode.cn/problems/find-the-town-judge
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function nearestValidPoint(x: number, y: number, points: number[][]): number {
2+
return points.reduce(
3+
([p, d], [a, b], i) =>
4+
(x === a || y === b) && Math.abs(x - a) + Math.abs(y - b) < d
5+
? [i, Math.abs(x - a) + Math.abs(y - b)]
6+
: [p, d],
7+
[-1, Infinity],
8+
)[0];
9+
}
10+
export default nearestValidPoint;

0 commit comments

Comments
 (0)