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 <= 104points[i].length == 21 <= x, y, ai, bi <= 104When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach means we will look at every single point to see if it shares either the same x or y coordinate as our starting point. We'll calculate the distance for each of these valid points and return the closest one.
Here's how the algorithm would work step-by-step:
def find_nearest_point_brute_force(starting_point, points):
shortest_distance = float('inf')
nearest_point_index = -1
# Iterate through each point in the list
for index, current_point in enumerate(points):
# Check if either x or y coordinate is the same
if (current_point[0] == starting_point[0] or\
current_point[1] == starting_point[1]):
# Calculate Manhattan distance
distance = abs(current_point[0] - starting_point[0]) +\
abs(current_point[1] - starting_point[1])
# Update shortest distance if necessary
if distance < shortest_distance:
# We found a new nearest point
shortest_distance = distance
nearest_point_index = index
return nearest_point_indexThe key idea is to only consider points that share either an x or y coordinate with our starting point and then find the closest one among them. We can avoid unnecessary calculations by ignoring points that aren't aligned along the same horizontal or vertical line. This significantly reduces the number of distances we need to calculate.
Here's how the algorithm would work step-by-step:
def find_nearest_valid_point(
x_coordinate, y_coordinate, points):
smallest_distance = float('inf')
nearest_point_index = -1
# Iterate through each point to find valid candidates
for index, (point_x, point_y) in enumerate(points):
if x_coordinate == point_x or y_coordinate == point_y:
# Calculate Manhattan distance for valid points
manhattan_distance = abs(x_coordinate - point_x) + \
abs(y_coordinate - point_y)
# Update nearest point if current distance is smaller
if manhattan_distance < smallest_distance:
smallest_distance = manhattan_distance
nearest_point_index = index
return nearest_point_index| Case | How to Handle |
|---|---|
| Empty points array | Return -1 immediately because no valid point can exist if the points array is empty. |
| Null points array | Return -1 immediately, treating null input as no valid points. |
| Points array containing null points | Skip the null point during iteration to prevent NullPointerException and continue to process valid points. |
| Very large number of points in the points array (scalability) | Ensure the solution iterates efficiently to avoid timeouts with large input sizes; consider using appropriate data structures like priority queues if necessary. |
| Points with the same X and Y coordinate as the location | Include these points in the distance calculation and index comparison to ensure the absolute nearest by Manhattan Distance is correctly identified. |
| Multiple points with the same minimum Manhattan distance | Maintain the smallest index among points with equal minimum Manhattan distance. |
| Integer overflow in Manhattan distance calculation | Consider using long type for distance calculation to prevent potential overflows if the coordinates are large. |
| All points are equidistant from the location and share X or Y coordinate | The solution should select the point with the smallest index among all points with the minimum Manhattan distance. |