Skip to content

Commit 7d8c5a7

Browse files
committed
Upload 200
1 parent f2412c1 commit 7d8c5a7

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!-- markdownlint-disable MD041 -->
22
[![eevveerryyddaayy release](https://img.shields.io/badge/eevveerryyddaayy-v1.3.0-blue.svg)](https://github.com/ggeerraarrdd/eevveerryyddaayy/)
3-
[![Solved](https://img.shields.io/badge/solved-199-green.svg)](#index)
3+
[![Solved](https://img.shields.io/badge/solved-200-green.svg)](#index)
44
<!-- markdownlint-enable MD041 -->
55

66
# SQL Everyday
@@ -233,6 +233,7 @@ Because this project necessitated a framework to enable consistent daily practic
233233
| 197 | [Get Highest Answer Rate Question](https://leetcode.com/problems/get-highest-answer-rate-question/) | [Solution](solutions/197_get_highest_answer_rate_question.md) | LeetCode | Medium | |
234234
| 198 | [Count Student Number in Departments](https://leetcode.com/problems/count-student-number-in-departments/description/) | [Solution](solutions/198_count_student_number_in_departments.md) | LeetCode | Medium | |
235235
| 199 | [Tree Node](https://leetcode.com/problems/tree-node/description/) | [Solution](solutions/199_tree_node.md) | LeetCode | Medium | |
236+
| 200 | [Shortest Distance in a Plane](https://leetcode.com/problems/shortest-distance-in-a-plane/description/) | [Solution](solutions/200_shortest_distance_in_a_plane.md) | LeetCode | Medium | `CROSS JOIN` |
236237
<!-- Index End - WARNING: Do not delete or modify this markdown comment. -->
237238
<!--- cSpell:enable --->
238239

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# SQL Everyday \#200
2+
3+
## Shortest Distance in a Plane
4+
5+
Site: LeetCode\
6+
Difficulty per Site: Medium
7+
8+
## Problem
9+
10+
The distance between two points `p1(x1, y1)` and `p2(x2, y2)` is `sqrt((x2 - x1)2 + (y2 - y1)2)`.
11+
12+
Write a solution to report the shortest distance between any two points from the `Point2D` table. Round the distance to **two decimal points**. [[Full Description](https://leetcode.com/problems/shortest-distance-in-a-plane/description/)]
13+
14+
## Submitted Solution
15+
16+
```sql
17+
-- Submitted Solution
18+
WITH cte AS (
19+
SELECT
20+
-- p1.x AS x_1
21+
-- ,p1.y AS y_1
22+
-- ,p2.x AS x_2
23+
-- ,p2.y AS y_2
24+
ROUND(SQRT( (p2.x - p1.x)^2 + (p2.y - p1.y)^2 )::numeric, 2) AS distances
25+
FROM Point2D as p1
26+
CROSS JOIN Point2D AS p2
27+
)
28+
SELECT
29+
MIN(distances) AS shortest
30+
FROM cte
31+
WHERE distances > 0
32+
;
33+
```
34+
35+
## Site Solution
36+
37+
```sql
38+
-- LeetCode Solution
39+
-- Approach 1: Using SQRT, POW() functions and math knowledge
40+
ELECT
41+
ROUND(SQRT(MIN((POW(p1.x - p2.x, 2) + POW(p1.y - p2.y, 2)))), 2) AS shortest
42+
FROM
43+
point_2d p1
44+
JOIN
45+
point_2d p2 ON p1.x != p2.x OR p1.y != p2.y
46+
;
47+
48+
-- Approach 2: Optimize to avoid reduplicate calculations
49+
SELECT
50+
ROUND(SQRT(MIN((POW(p1.x - p2.x, 2) + POW(p1.y - p2.y, 2)))),2) AS shortest
51+
FROM
52+
point_2d p1
53+
JOIN
54+
point_2d p2 ON (p1.x <= p2.x AND p1.y < p2.y)
55+
OR (p1.x <= p2.x AND p1.y > p2.y)
56+
OR (p1.x < p2.x AND p1.y = p2.y)
57+
;
58+
```
59+
60+
## Notes
61+
62+
TBD
63+
64+
## NB
65+
66+
`CROSS JOIN`
67+
68+
Go to [Index](../?tab=readme-ov-file#index)\
69+
Go to [Overview](../?tab=readme-ov-file)

0 commit comments

Comments
 (0)