From a244bc3b2c5886a771ffffdeda3a15efd1be2418 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 28 Feb 2024 08:37:21 +0800 Subject: [PATCH 1/2] feat: add solutions to lc problems: No.3053~3056 --- .../README.md | 25 +++++++++++++++++-- .../README_EN.md | 25 +++++++++++++++++-- .../Solution.sql | 12 +++++++++ .../3054.Binary Tree Nodes/README.md | 15 +++++++++-- .../3054.Binary Tree Nodes/README_EN.md | 15 +++++++++-- .../3054.Binary Tree Nodes/Solution.sql | 8 ++++++ .../3055.Top Percentile Fraud/README.md | 20 +++++++++++++-- .../3055.Top Percentile Fraud/README_EN.md | 20 +++++++++++++-- .../3055.Top Percentile Fraud/Solution.sql | 15 +++++++++++ .../3000-3099/3056.Snaps Analysis/README.md | 14 +++++++++-- .../3056.Snaps Analysis/README_EN.md | 14 +++++++++-- .../3056.Snaps Analysis/Solution.sql | 9 +++++++ 12 files changed, 176 insertions(+), 16 deletions(-) create mode 100644 solution/3000-3099/3053.Classifying Triangles by Lengths/Solution.sql create mode 100644 solution/3000-3099/3054.Binary Tree Nodes/Solution.sql create mode 100644 solution/3000-3099/3055.Top Percentile Fraud/Solution.sql create mode 100644 solution/3000-3099/3056.Snaps Analysis/Solution.sql diff --git a/solution/3000-3099/3053.Classifying Triangles by Lengths/README.md b/solution/3000-3099/3053.Classifying Triangles by Lengths/README.md index 57ee4933f0f62..c0900658db248 100644 --- a/solution/3000-3099/3053.Classifying Triangles by Lengths/README.md +++ b/solution/3000-3099/3053.Classifying Triangles by Lengths/README.md @@ -66,12 +66,33 @@ Triangles table: ## 解法 -### 方法一 +### 方法一:使用 CASE WHEN 语句 + +我们可以使用 `CASE WHEN` 语句来判断三角形的类型。 + +首先,我们需要判断三个边是否能够构成一个三角形。如果不能,我们返回 `Not A Triangle`。 + +然后,我们判断三个边的长度是否相等。如果相等,我们返回 `Equilateral`。 + +接着,我们判断是否有两个边的长度相等。如果有,我们返回 `Isosceles`。 + +否则,说明三个边的长度都不相等,我们返回 `Scalene`。 ```sql - +# Write your MySQL query statement below +SELECT + CASE + WHEN A + B <= C + OR A + C <= B + OR B + C <= A THEN 'Not A Triangle' + WHEN A = B + AND B = c THEN 'Equilateral' + WHEN (A = B) + (B = C) + (A = C) = 1 THEN 'Isosceles' + ELSE 'Scalene' + END AS triangle_type +FROM Triangles; ``` diff --git a/solution/3000-3099/3053.Classifying Triangles by Lengths/README_EN.md b/solution/3000-3099/3053.Classifying Triangles by Lengths/README_EN.md index bbda4c69cb6b3..b882652dcbaab 100644 --- a/solution/3000-3099/3053.Classifying Triangles by Lengths/README_EN.md +++ b/solution/3000-3099/3053.Classifying Triangles by Lengths/README_EN.md @@ -64,12 +64,33 @@ Triangles table: ## Solutions -### Solution 1 +### Solution 1: Using CASE WHEN Statement + +We can use the `CASE WHEN` statement to determine the type of the triangle. + +First, we need to determine whether the three sides can form a triangle. If not, we return `Not A Triangle`. + +Then, we check if the lengths of the three sides are equal. If they are, we return `Equilateral`. + +Next, we check if there are two sides with equal length. If there are, we return `Isosceles`. + +Otherwise, it means that the lengths of the three sides are all different, so we return `Scalene`. ```sql - +# Write your MySQL query statement below +SELECT + CASE + WHEN A + B <= C + OR A + C <= B + OR B + C <= A THEN 'Not A Triangle' + WHEN A = B + AND B = c THEN 'Equilateral' + WHEN (A = B) + (B = C) + (A = C) = 1 THEN 'Isosceles' + ELSE 'Scalene' + END AS triangle_type +FROM Triangles; ``` diff --git a/solution/3000-3099/3053.Classifying Triangles by Lengths/Solution.sql b/solution/3000-3099/3053.Classifying Triangles by Lengths/Solution.sql new file mode 100644 index 0000000000000..390a1521e5cd1 --- /dev/null +++ b/solution/3000-3099/3053.Classifying Triangles by Lengths/Solution.sql @@ -0,0 +1,12 @@ +# Write your MySQL query statement below +SELECT + CASE + WHEN A + B <= C + OR A + C <= B + OR B + C <= A THEN 'Not A Triangle' + WHEN A = B + AND B = c THEN 'Equilateral' + WHEN (A = B) + (B = C) + (A = C) = 1 THEN 'Isosceles' + ELSE 'Scalene' + END AS triangle_type +FROM Triangles; diff --git a/solution/3000-3099/3054.Binary Tree Nodes/README.md b/solution/3000-3099/3054.Binary Tree Nodes/README.md index c2a05dfddddc5..875ede6763482 100644 --- a/solution/3000-3099/3054.Binary Tree Nodes/README.md +++ b/solution/3000-3099/3054.Binary Tree Nodes/README.md @@ -70,12 +70,23 @@ Tree table: ## 解法 -### 方法一 +### 方法一:左连接 + +如果一个节点的父节点为空,则它是根节点;如果一个节点不是任何节点的父节点,则它是叶子节点;否则它是内部节点。 + +因此,我们使用左连接来连接两次 `Tree` 表,连接条件是 `t1.N = t2.P`。那么如果 `t1.P` 为空,则 `t1.N` 是根节点;如果 `t2.P` 为空,则 `t1.N` 是叶子节点;否则 `t1.N` 是内部节点。 ```sql - +# Write your MySQL query statement below +SELECT DISTINCT + t1.N AS N, + IF(t1.P IS NULL, 'Root', IF(t2.P IS NULL, 'Leaf', 'Inner')) AS Type +FROM + Tree AS t1 + LEFT JOIN Tree AS t2 ON t1.N = t2.p +ORDER BY 1; ``` diff --git a/solution/3000-3099/3054.Binary Tree Nodes/README_EN.md b/solution/3000-3099/3054.Binary Tree Nodes/README_EN.md index 1b55b087cd429..1780ed376172d 100644 --- a/solution/3000-3099/3054.Binary Tree Nodes/README_EN.md +++ b/solution/3000-3099/3054.Binary Tree Nodes/README_EN.md @@ -68,12 +68,23 @@ Tree table: ## Solutions -### Solution 1 +### Solution 1: Left Join + +If a node's parent is null, then it is a root node; if a node is not the parent of any node, then it is a leaf node; otherwise, it is an internal node. + +Therefore, we use left join to join the `Tree` table twice, with the join condition being `t1.N = t2.P`. If `t1.P` is null, then `t1.N` is a root node; if `t2.P` is null, then `t1.N` is a leaf node; otherwise, `t1.N` is an internal node. ```sql - +# Write your MySQL query statement below +SELECT DISTINCT + t1.N AS N, + IF(t1.P IS NULL, 'Root', IF(t2.P IS NULL, 'Leaf', 'Inner')) AS Type +FROM + Tree AS t1 + LEFT JOIN Tree AS t2 ON t1.N = t2.p +ORDER BY 1; ``` diff --git a/solution/3000-3099/3054.Binary Tree Nodes/Solution.sql b/solution/3000-3099/3054.Binary Tree Nodes/Solution.sql new file mode 100644 index 0000000000000..745f4baedd5da --- /dev/null +++ b/solution/3000-3099/3054.Binary Tree Nodes/Solution.sql @@ -0,0 +1,8 @@ +# Write your MySQL query statement below +SELECT DISTINCT + t1.N AS N, + IF(t1.P IS NULL, 'Root', IF(t2.P IS NULL, 'Leaf', 'Inner')) AS Type +FROM + Tree AS t1 + LEFT JOIN Tree AS t2 ON t1.N = t2.p +ORDER BY 1; diff --git a/solution/3000-3099/3055.Top Percentile Fraud/README.md b/solution/3000-3099/3055.Top Percentile Fraud/README.md index fcebde121c68f..762c39d084b1a 100644 --- a/solution/3000-3099/3055.Top Percentile Fraud/README.md +++ b/solution/3000-3099/3055.Top Percentile Fraud/README.md @@ -73,12 +73,28 @@ Output table is ordered by state in ascending order, fraud score in descending o ## 解法 -### 方法一 +### 方法一:使用窗口函数 + +我们可以使用 `RANK()` 窗口函数来计算每个州的欺诈分数的排名,然后筛选出排名为 1 的记录,并且按照题目要求排序。 ```sql - +# Write your MySQL query statement below +WITH + T AS ( + SELECT + *, + RANK() OVER ( + PARTITION BY state + ORDER BY fraud_score DESC + ) AS rk + FROM Fraud + ) +SELECT policy_id, state, fraud_score +FROM T +WHERE rk = 1 +ORDER BY 2, 3 DESC, 1; ``` diff --git a/solution/3000-3099/3055.Top Percentile Fraud/README_EN.md b/solution/3000-3099/3055.Top Percentile Fraud/README_EN.md index 29546523f1c18..37b9acd4951c5 100644 --- a/solution/3000-3099/3055.Top Percentile Fraud/README_EN.md +++ b/solution/3000-3099/3055.Top Percentile Fraud/README_EN.md @@ -71,12 +71,28 @@ Output table is ordered by state in ascending order, fraud score in descending o ## Solutions -### Solution 1 +### Solution 1: Using Window Function + +We can use the `RANK()` window function to calculate the ranking of fraud scores for each state, then filter out the records with a rank of 1, and sort them as required by the problem. ```sql - +# Write your MySQL query statement below +WITH + T AS ( + SELECT + *, + RANK() OVER ( + PARTITION BY state + ORDER BY fraud_score DESC + ) AS rk + FROM Fraud + ) +SELECT policy_id, state, fraud_score +FROM T +WHERE rk = 1 +ORDER BY 2, 3 DESC, 1; ``` diff --git a/solution/3000-3099/3055.Top Percentile Fraud/Solution.sql b/solution/3000-3099/3055.Top Percentile Fraud/Solution.sql new file mode 100644 index 0000000000000..86f580fb828e3 --- /dev/null +++ b/solution/3000-3099/3055.Top Percentile Fraud/Solution.sql @@ -0,0 +1,15 @@ +# Write your MySQL query statement below +WITH + T AS ( + SELECT + *, + RANK() OVER ( + PARTITION BY state + ORDER BY fraud_score DESC + ) AS rk + FROM Fraud + ) +SELECT policy_id, state, fraud_score +FROM T +WHERE rk = 1 +ORDER BY 2, 3 DESC, 1; diff --git a/solution/3000-3099/3056.Snaps Analysis/README.md b/solution/3000-3099/3056.Snaps Analysis/README.md index 132e9cab618f7..cba12f7749a1d 100644 --- a/solution/3000-3099/3056.Snaps Analysis/README.md +++ b/solution/3000-3099/3056.Snaps Analysis/README.md @@ -98,12 +98,22 @@ All percentages in output table rounded to the two decimal places. ## 解法 -### 方法一 +### 方法一:等值连接 + 分组求和 + +我们可以通过等值连接,将 `Activities` 表和 `Age` 表按照 `user_id` 进行连接,然后再按照 `age_bucket` 进行分组,最后计算每个年龄段的发送和打开的百分比。 ```sql - +# Write your MySQL query statement below +SELECT + age_bucket, + ROUND(100 * SUM(IF(activity_type = 'send', time_spent, 0)) / SUM(time_spent), 2) AS send_perc, + ROUND(100 * SUM(IF(activity_type = 'open', time_spent, 0)) / SUM(time_spent), 2) AS open_perc +FROM + Activities + JOIN Age USING (user_id) +GROUP BY age_bucket; ``` diff --git a/solution/3000-3099/3056.Snaps Analysis/README_EN.md b/solution/3000-3099/3056.Snaps Analysis/README_EN.md index 38a154160482c..701f73e88d4a6 100644 --- a/solution/3000-3099/3056.Snaps Analysis/README_EN.md +++ b/solution/3000-3099/3056.Snaps Analysis/README_EN.md @@ -96,12 +96,22 @@ All percentages in output table rounded to the two decimal places. ## Solutions -### Solution 1 +### Solution 1: Equi-Join + Group By Summation + +We can perform an equi-join to connect the `Activities` table and the `Age` table based on `user_id`. Then, group by `age_bucket` and finally calculate the percentage of sends and opens for each age group. ```sql - +# Write your MySQL query statement below +SELECT + age_bucket, + ROUND(100 * SUM(IF(activity_type = 'send', time_spent, 0)) / SUM(time_spent), 2) AS send_perc, + ROUND(100 * SUM(IF(activity_type = 'open', time_spent, 0)) / SUM(time_spent), 2) AS open_perc +FROM + Activities + JOIN Age USING (user_id) +GROUP BY age_bucket; ``` diff --git a/solution/3000-3099/3056.Snaps Analysis/Solution.sql b/solution/3000-3099/3056.Snaps Analysis/Solution.sql new file mode 100644 index 0000000000000..0e7cd7a12db36 --- /dev/null +++ b/solution/3000-3099/3056.Snaps Analysis/Solution.sql @@ -0,0 +1,9 @@ +# Write your MySQL query statement below +SELECT + age_bucket, + ROUND(100 * SUM(IF(activity_type = 'send', time_spent, 0)) / SUM(time_spent), 2) AS send_perc, + ROUND(100 * SUM(IF(activity_type = 'open', time_spent, 0)) / SUM(time_spent), 2) AS open_perc +FROM + Activities + JOIN Age USING (user_id) +GROUP BY age_bucket; From 88a1d049cc0cb3e18d3541350bc8deaca50c5b6e Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 28 Feb 2024 08:43:49 +0800 Subject: [PATCH 2/2] feat: update --- solution/3000-3099/3056.Snaps Analysis/README.md | 2 +- solution/3000-3099/3056.Snaps Analysis/README_EN.md | 2 +- solution/3000-3099/3056.Snaps Analysis/Solution.sql | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/solution/3000-3099/3056.Snaps Analysis/README.md b/solution/3000-3099/3056.Snaps Analysis/README.md index cba12f7749a1d..f15d55f46337c 100644 --- a/solution/3000-3099/3056.Snaps Analysis/README.md +++ b/solution/3000-3099/3056.Snaps Analysis/README.md @@ -113,7 +113,7 @@ SELECT FROM Activities JOIN Age USING (user_id) -GROUP BY age_bucket; +GROUP BY 1; ``` diff --git a/solution/3000-3099/3056.Snaps Analysis/README_EN.md b/solution/3000-3099/3056.Snaps Analysis/README_EN.md index 701f73e88d4a6..e5130bfd3203d 100644 --- a/solution/3000-3099/3056.Snaps Analysis/README_EN.md +++ b/solution/3000-3099/3056.Snaps Analysis/README_EN.md @@ -111,7 +111,7 @@ SELECT FROM Activities JOIN Age USING (user_id) -GROUP BY age_bucket; +GROUP BY 1; ``` diff --git a/solution/3000-3099/3056.Snaps Analysis/Solution.sql b/solution/3000-3099/3056.Snaps Analysis/Solution.sql index 0e7cd7a12db36..31ee1b9aa4ba5 100644 --- a/solution/3000-3099/3056.Snaps Analysis/Solution.sql +++ b/solution/3000-3099/3056.Snaps Analysis/Solution.sql @@ -6,4 +6,4 @@ SELECT FROM Activities JOIN Age USING (user_id) -GROUP BY age_bucket; +GROUP BY 1;