Shortest Distance In A Plane Problem


Description

LeetCode Problem 612.

Table point_2d holds the coordinates (x,y) of some unique points (more than two) in a plane.

Write a query to find the shortest distance between these points rounded to 2 decimals.

1
2
3
4
5
| x  | y  |
|----|----|
| -1 | -1 |
| 0  | 0  |
| -1 | -2 |

The shortest distance is 1.00 from point (-1,-1) to (-1,2). So the output should be:

1
2
3
| shortest |
|----------|
| 1.00     |


MySQL Solution

1
2
3
4
select round(sqrt(min((pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2)))), 2) as shortest
from point_2d p1
join point_2d p2 
on p1.x != p2.x or p1.y != p2.y




Related Posts

Students Report By Geography Problem

LeetCode 618. A U.S graduate school has students from Asia,...

Average Salary: Departments VS Company Problem

LeetCode 615. Given two tables as below, write a query...

Second Degree Follower Problem

LeetCode 614. Please write a sql query to get the...

Shortest Distance In A Plane Problem

LeetCode 612. Write a query to find the shortest distance...

Triangle Judgement Problem

LeetCode 610. A pupil Tim gets homework to identify whether...

Shortest Distance Problem

LeetCode 613. Write a query to find the shortest distance...