Skip to content

Commit 33c4010

Browse files
authored
feat: add solutions to lc problems: No.3053~3056 (doocs#2383)
1 parent afa7067 commit 33c4010

File tree

12 files changed

+176
-16
lines changed

12 files changed

+176
-16
lines changed

solution/3000-3099/3053.Classifying Triangles by Lengths/README.md

+23-2
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,33 @@ Triangles table:
6666

6767
## 解法
6868

69-
### 方法一
69+
### 方法一:使用 CASE WHEN 语句
70+
71+
我们可以使用 `CASE WHEN` 语句来判断三角形的类型。
72+
73+
首先,我们需要判断三个边是否能够构成一个三角形。如果不能,我们返回 `Not A Triangle`
74+
75+
然后,我们判断三个边的长度是否相等。如果相等,我们返回 `Equilateral`
76+
77+
接着,我们判断是否有两个边的长度相等。如果有,我们返回 `Isosceles`
78+
79+
否则,说明三个边的长度都不相等,我们返回 `Scalene`
7080

7181
<!-- tabs:start -->
7282

7383
```sql
74-
84+
# Write your MySQL query statement below
85+
SELECT
86+
CASE
87+
WHEN A + B <= C
88+
OR A + C <= B
89+
OR B + C <= A THEN 'Not A Triangle'
90+
WHEN A = B
91+
AND B = c THEN 'Equilateral'
92+
WHEN (A = B) + (B = C) + (A = C) = 1 THEN 'Isosceles'
93+
ELSE 'Scalene'
94+
END AS triangle_type
95+
FROM Triangles;
7596
```
7697

7798
<!-- tabs:end -->

solution/3000-3099/3053.Classifying Triangles by Lengths/README_EN.md

+23-2
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,33 @@ Triangles table:
6464

6565
## Solutions
6666

67-
### Solution 1
67+
### Solution 1: Using CASE WHEN Statement
68+
69+
We can use the `CASE WHEN` statement to determine the type of the triangle.
70+
71+
First, we need to determine whether the three sides can form a triangle. If not, we return `Not A Triangle`.
72+
73+
Then, we check if the lengths of the three sides are equal. If they are, we return `Equilateral`.
74+
75+
Next, we check if there are two sides with equal length. If there are, we return `Isosceles`.
76+
77+
Otherwise, it means that the lengths of the three sides are all different, so we return `Scalene`.
6878

6979
<!-- tabs:start -->
7080

7181
```sql
72-
82+
# Write your MySQL query statement below
83+
SELECT
84+
CASE
85+
WHEN A + B <= C
86+
OR A + C <= B
87+
OR B + C <= A THEN 'Not A Triangle'
88+
WHEN A = B
89+
AND B = c THEN 'Equilateral'
90+
WHEN (A = B) + (B = C) + (A = C) = 1 THEN 'Isosceles'
91+
ELSE 'Scalene'
92+
END AS triangle_type
93+
FROM Triangles;
7394
```
7495

7596
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Write your MySQL query statement below
2+
SELECT
3+
CASE
4+
WHEN A + B <= C
5+
OR A + C <= B
6+
OR B + C <= A THEN 'Not A Triangle'
7+
WHEN A = B
8+
AND B = c THEN 'Equilateral'
9+
WHEN (A = B) + (B = C) + (A = C) = 1 THEN 'Isosceles'
10+
ELSE 'Scalene'
11+
END AS triangle_type
12+
FROM Triangles;

solution/3000-3099/3054.Binary Tree Nodes/README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,23 @@ Tree table:
7070

7171
## 解法
7272

73-
### 方法一
73+
### 方法一:左连接
74+
75+
如果一个节点的父节点为空,则它是根节点;如果一个节点不是任何节点的父节点,则它是叶子节点;否则它是内部节点。
76+
77+
因此,我们使用左连接来连接两次 `Tree` 表,连接条件是 `t1.N = t2.P`。那么如果 `t1.P` 为空,则 `t1.N` 是根节点;如果 `t2.P` 为空,则 `t1.N` 是叶子节点;否则 `t1.N` 是内部节点。
7478

7579
<!-- tabs:start -->
7680

7781
```sql
78-
82+
# Write your MySQL query statement below
83+
SELECT DISTINCT
84+
t1.N AS N,
85+
IF(t1.P IS NULL, 'Root', IF(t2.P IS NULL, 'Leaf', 'Inner')) AS Type
86+
FROM
87+
Tree AS t1
88+
LEFT JOIN Tree AS t2 ON t1.N = t2.p
89+
ORDER BY 1;
7990
```
8091

8192
<!-- tabs:end -->

solution/3000-3099/3054.Binary Tree Nodes/README_EN.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,23 @@ Tree table:
6868

6969
## Solutions
7070

71-
### Solution 1
71+
### Solution 1: Left Join
72+
73+
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.
74+
75+
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.
7276

7377
<!-- tabs:start -->
7478

7579
```sql
76-
80+
# Write your MySQL query statement below
81+
SELECT DISTINCT
82+
t1.N AS N,
83+
IF(t1.P IS NULL, 'Root', IF(t2.P IS NULL, 'Leaf', 'Inner')) AS Type
84+
FROM
85+
Tree AS t1
86+
LEFT JOIN Tree AS t2 ON t1.N = t2.p
87+
ORDER BY 1;
7788
```
7889

7990
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Write your MySQL query statement below
2+
SELECT DISTINCT
3+
t1.N AS N,
4+
IF(t1.P IS NULL, 'Root', IF(t2.P IS NULL, 'Leaf', 'Inner')) AS Type
5+
FROM
6+
Tree AS t1
7+
LEFT JOIN Tree AS t2 ON t1.N = t2.p
8+
ORDER BY 1;

solution/3000-3099/3055.Top Percentile Fraud/README.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,28 @@ Output table is ordered by state in ascending order, fraud score in descending o
7373

7474
## 解法
7575

76-
### 方法一
76+
### 方法一:使用窗口函数
77+
78+
我们可以使用 `RANK()` 窗口函数来计算每个州的欺诈分数的排名,然后筛选出排名为 1 的记录,并且按照题目要求排序。
7779

7880
<!-- tabs:start -->
7981

8082
```sql
81-
83+
# Write your MySQL query statement below
84+
WITH
85+
T AS (
86+
SELECT
87+
*,
88+
RANK() OVER (
89+
PARTITION BY state
90+
ORDER BY fraud_score DESC
91+
) AS rk
92+
FROM Fraud
93+
)
94+
SELECT policy_id, state, fraud_score
95+
FROM T
96+
WHERE rk = 1
97+
ORDER BY 2, 3 DESC, 1;
8298
```
8399

84100
<!-- tabs:end -->

solution/3000-3099/3055.Top Percentile Fraud/README_EN.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,28 @@ Output table is ordered by state in ascending order, fraud score in descending o
7171

7272
## Solutions
7373

74-
### Solution 1
74+
### Solution 1: Using Window Function
75+
76+
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.
7577

7678
<!-- tabs:start -->
7779

7880
```sql
79-
81+
# Write your MySQL query statement below
82+
WITH
83+
T AS (
84+
SELECT
85+
*,
86+
RANK() OVER (
87+
PARTITION BY state
88+
ORDER BY fraud_score DESC
89+
) AS rk
90+
FROM Fraud
91+
)
92+
SELECT policy_id, state, fraud_score
93+
FROM T
94+
WHERE rk = 1
95+
ORDER BY 2, 3 DESC, 1;
8096
```
8197

8298
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT
5+
*,
6+
RANK() OVER (
7+
PARTITION BY state
8+
ORDER BY fraud_score DESC
9+
) AS rk
10+
FROM Fraud
11+
)
12+
SELECT policy_id, state, fraud_score
13+
FROM T
14+
WHERE rk = 1
15+
ORDER BY 2, 3 DESC, 1;

solution/3000-3099/3056.Snaps Analysis/README.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,22 @@ All percentages in output table rounded to the two decimal places.
9898

9999
## 解法
100100

101-
### 方法一
101+
### 方法一:等值连接 + 分组求和
102+
103+
我们可以通过等值连接,将 `Activities` 表和 `Age` 表按照 `user_id` 进行连接,然后再按照 `age_bucket` 进行分组,最后计算每个年龄段的发送和打开的百分比。
102104

103105
<!-- tabs:start -->
104106

105107
```sql
106-
108+
# Write your MySQL query statement below
109+
SELECT
110+
age_bucket,
111+
ROUND(100 * SUM(IF(activity_type = 'send', time_spent, 0)) / SUM(time_spent), 2) AS send_perc,
112+
ROUND(100 * SUM(IF(activity_type = 'open', time_spent, 0)) / SUM(time_spent), 2) AS open_perc
113+
FROM
114+
Activities
115+
JOIN Age USING (user_id)
116+
GROUP BY 1;
107117
```
108118

109119
<!-- tabs:end -->

solution/3000-3099/3056.Snaps Analysis/README_EN.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,22 @@ All percentages in output table rounded to the two decimal places.
9696

9797
## Solutions
9898

99-
### Solution 1
99+
### Solution 1: Equi-Join + Group By Summation
100+
101+
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.
100102

101103
<!-- tabs:start -->
102104

103105
```sql
104-
106+
# Write your MySQL query statement below
107+
SELECT
108+
age_bucket,
109+
ROUND(100 * SUM(IF(activity_type = 'send', time_spent, 0)) / SUM(time_spent), 2) AS send_perc,
110+
ROUND(100 * SUM(IF(activity_type = 'open', time_spent, 0)) / SUM(time_spent), 2) AS open_perc
111+
FROM
112+
Activities
113+
JOIN Age USING (user_id)
114+
GROUP BY 1;
105115
```
106116

107117
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Write your MySQL query statement below
2+
SELECT
3+
age_bucket,
4+
ROUND(100 * SUM(IF(activity_type = 'send', time_spent, 0)) / SUM(time_spent), 2) AS send_perc,
5+
ROUND(100 * SUM(IF(activity_type = 'open', time_spent, 0)) / SUM(time_spent), 2) AS open_perc
6+
FROM
7+
Activities
8+
JOIN Age USING (user_id)
9+
GROUP BY 1;

0 commit comments

Comments
 (0)