Find Nearest Point That Has the Same X or Y Coordinate — LeetCode #1779 (Easy)
Tags: Array
Related patterns: Hash Map, Array
Brute Force approach
Time complexity: O(n). Space complexity: O(1).
In this approach, we will check each point in the array to see if it shares the same x or y coordinate with our current location. If it does, we will calculate the Manhattan distance and keep track of the smallest distance found.
The time complexity is O(n) because we iterate through the list of points once. The space complexity is O(1) since we only use a few variables to store the minimum distance and index.
- Step 1: Initialize a variable to track the minimum distance and the index of the closest point.
- Step 2: Iterate through each point in the array.
- Step 3: For each point, check if it shares the same x or y coordinate with the current location.
- Step 4: If it does, calculate the Manhattan distance and update the minimum distance and index if this distance is smaller.
- Step 5: After checking all points, return the index of the closest point or -1 if no valid points were found.
1. Start with x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]].
2. Initialize min_distance = inf, min_index = -1.
3. Check point [1,2]: No match, continue.
4. Check point [3,1]: Match (x), distance = 3, update min_distance = 3, min_index = 1.
5. Check point [2,4]: Match (y), distance = 1, update min_distance = 1, min_index = 2.
6. Check point [2,3]: No match, continue. Check point [4,4]: Match (y), distance = 1, but min_index remains 2.
7. Return min_index = 2.
Optimal Solution approach
Time complexity: O(n). Space complexity: O(1).
This approach is similar to the brute force method but focuses on reducing unnecessary calculations. By maintaining a single pass through the points and directly calculating distances only for valid points, we ensure efficiency.
The time complexity remains O(n) as we still iterate through the points array once. The space complexity is O(1) because we only use a few variables to track the minimum distance and index.
- Step 1: Initialize variables for minimum distance and index.
- Step 2: Loop through the points array.
- Step 3: For each point, check if it shares the x or y coordinate with the current location.
- Step 4: If valid, calculate the Manhattan distance and update the minimum distance and index accordingly.
- Step 5: Return the index of the closest valid point or -1 if none found.
1. Start with x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]].
2. Initialize min_distance = inf, min_index = -1.
3. Check point [1,2]: No match, continue.
4. Check point [3,1]: Match (x), distance = 3, update min_distance = 3, min_index = 1.
5. Check point [2,4]: Match (y), distance = 1, update min_distance = 1, min_index = 2.
6. Check point [2,3]: No match, continue. Check point [4,4]: Match (y), distance = 1, but min_index remains 2.
7. Return min_index = 2.
Key Insights
- Valid points are those that share either the x or y coordinate with the current location.
- Manhattan distance is calculated as the sum of the absolute differences of the coordinates.
Common Mistakes
- Not checking both x and y coordinates for validity.
- Forgetting to handle the case where no valid points exist.
Interview Tips
- Clearly explain your thought process as you iterate through the points.
- Consider edge cases, such as when the current location is the same as a point.
- Optimize your solution after implementing the brute force method to show your understanding of efficiency.