[LeetCode] 1779. Find Nearest Point That Has the Same X or Y Coordinate
You are given two integers, x
and y
, which represent your current location on a Cartesian grid: (x, y)
. You are also given an array points
where each points[i] = [ai, bi]
represents that a point exists at (ai, bi)
. A point is valid if it shares the same x-coordinate or the same y-coordinate as your location.
Return the index (0-indexed) of the valid point with the smallest Manhattan distance from your current location. If there are multiple, return the valid point with the smallest index. If there are no valid points, return -1
.
The Manhattan distance between two points (x1, y1)
and (x2, y2)
is abs(x1 - x2) + abs(y1 - y2)
.
Example 1:
Input: x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]] Output: 2 Explanation: Of all the points, only [3,1], [2,4] and [4,4] are valid. Of the valid points, [2,4] and [4,4] have the smallest Manhattan distance from your current location, with a distance of 1. [2,4] has the smallest index, so return 2.
Example 2:
Input: x = 3, y = 4, points = [[3,4]] Output: 0 Explanation: The answer is allowed to be on the same location as your current location.
Example 3:
Input: x = 3, y = 4, points = [[2,3]] Output: -1 Explanation: There are no valid points.
Constraints:
1 <= points.length <= 104
points[i].length == 2
1 <= x, y, ai, bi <= 104
找到最近的有相同 X 或 Y 坐标的点。
给你两个整数 x 和 y ,表示你在一个笛卡尔坐标系下的 (x, y) 处。同时,在同一个坐标系下给你一个数组 points ,其中 points[i] = [ai, bi] 表示在 (ai, bi) 处有一个点。当一个点与你所在的位置有相同的 x 坐标或者相同的 y 坐标时,我们称这个点是 有效的 。
请返回距离你当前位置 曼哈顿距离 最近的 有效 点的下标(下标从 0 开始)。如果有多个最近的有效点,请返回下标 最小 的一个。如果没有有效点,请返回 -1 。
两个点 (x1, y1) 和 (x2, y2) 之间的 曼哈顿距离 为 abs(x1 - x2) + abs(y1 - y2) 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题意不难理解,考察如何计算曼哈顿距离,唯一需要注意的是返回的是满足题意的点在 points 数组中的下标。
时间O(n)
空间O(1)
Java实现
1 class Solution { 2 public int nearestValidPoint(int x, int y, int[][] points) { 3 int minManhattan = Integer.MAX_VALUE; 4 int index = -1; 5 for (int i = 0; i < points.length; i++) { 6 int[] p = points[i]; 7 if (p[0] == x || p[1] == y) { 8 int curManhattan = Math.abs(p[0] - x) + Math.abs(p[1] - y); 9 if (curManhattan < minManhattan) { 10 minManhattan = curManhattan; 11 index = i; 12 } 13 } 14 } 15 return index; 16 } 17 }