You have a table Point2D that stores coordinates of points on a 2D plane. Each row represents a point with its (x, y) coordinates.
Your task: Find the shortest distance between any two points in the table.
The distance between two points p1(x1, y1) and p2(x2, y2) is calculated using the Euclidean distance formula: sqrt((x2 - x1)² + (y2 - y1)²)
Requirements:
- Round the result to 2 decimal places
- Return only the shortest distance value
- Handle cases where multiple point pairs have the same minimum distance
Table Schema
| Column Name | Type | Description |
|---|---|---|
x
PK
|
int | X coordinate of the point |
y
PK
|
int | Y coordinate of the point |
Input & Output
| x | y |
|---|---|
| -1 | -1 |
| 0 | 0 |
| -1 | 1 |
| shortest |
|---|
| 1.41 |
We have three points: (-1,-1), (0,0), and (-1,1). The distances are:
- (-1,-1) to (0,0): √[(0-(-1))² + (0-(-1))²] = √2 ≈ 1.41
- (-1,-1) to (-1,1): √[(-1-(-1))² + (1-(-1))²] = √4 = 2.00
- (0,0) to (-1,1): √[(-1-0)² + (1-0)²] = √2 ≈ 1.41
The minimum distance is 1.41.
| x | y |
|---|---|
| 0 | 0 |
| 3 | 4 |
| shortest |
|---|
| 5 |
With only two points (0,0) and (3,4), there's only one distance to calculate:
Distance = √[(3-0)² + (4-0)²] = √[9 + 16] = √25 = 5.00
| x | y |
|---|---|
| 0 | 0 |
| 1 | 0 |
| 0 | 1 |
| 2 | 2 |
| shortest |
|---|
| 1 |
Multiple pairs have distance 1.00: (0,0) to (1,0) and (0,0) to (0,1). Both are horizontal/vertical distances of 1 unit, which is the minimum among all possible pairs.
Constraints
-
2 ≤ number of points ≤ 500 -
-10^4 ≤ x, y ≤ 10^4 - All points have distinct coordinates
- At least one pair of points exists