Last updated on January 21st, 2025 at 10:05 pm
Here, we see the Shortest Distance in a Line LeetCode Solution. This Leetcode problem is solved using MySQL and Pandas.
List of all LeetCode Solution
Level of Question
Medium
Shortest Distance in a Line LeetCode Solution
Table of Contents
1. Problem Statement
Table point holds the x coordinate of some points on x-axis in a plane, which are all integers.
Write a query to find the shortest distance between two points in these points.
x |
-1 |
0 |
2 |
The shortest distance is ‘1’ obviously, which is from point ‘-1’ to ‘0’. So the output is as below:
shortest |
1 |
Note: Every point is unique, which means there is no duplicates in the table point.
2. Code Implementation in Different Languages
2.1 Shortest Distance in a Line MySQL
select min( abs(a.x - b.x) ) as shortest from point as a, point as b where a.x != b.x;