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
LeetCode Database - Medium
LeetCode 177
LeetCode 178
LeetCode 180
LeetCode 184
LeetCode 534
LeetCode 550
LeetCode 570
LeetCode 574
LeetCode 578
LeetCode 580
LeetCode 585
LeetCode 602
LeetCode 608
LeetCode 612
LeetCode 614
LeetCode 626
LeetCode 1045
LeetCode 1070
LeetCode 1077
LeetCode 1098
LeetCode 1107
LeetCode 1112
LeetCode 1126
More LeetCode Database
MySQL Tutorials