81. Minimum Distance Between Points
Imagine you're building a mapping or sensor system that tracks positions along a straight line (like the X-axis). For optimization or collision detection, you need to find the closest two data points in terms of distance. This is a common operation in spatial analysis and robotics path planning.
You are given a table Point:
āāāāāāāāāāāāāāā¦āāāāāāāāāāā ā Column Name ā Type ā ā āāāāāāāāāāāāāā¬āāāāāāāāāā⣠ā x ā int ā āāāāāāāāāāāāāāā©āāāāāāāāāāā
- x: X-coordinate of a point on the X-axis. It is the primary key, so all values are unique.
Write a query to find the shortest distance between any two points listed in the Point table.
Example 1:
Example:
Input:
Point Table
āāāāāāā ā x ā ā āāāāā⣠ā -1 ā āāāāāāā ā 0 ā āāāāāāā ā 2 ā āāāāāāā
Expected Output:
āāāāāāāāāāāāā ā shortest ā ā āāāāāāāāāāā⣠ā 1 ā āāāāāāāāāāāāā
Explanation:
- Distances: |(-1) - 0| = 1, |(-1) - 2| = 3, |0 - 2| = 2
- The minimum distance is 1.
Still unsure what the problem is asking ?
Letās go through a few more examples, step by step, to make it clearer.