Skip to content

feat: add solutions to lc problems: No.3053~3056 #2383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions solution/3000-3099/3053.Classifying Triangles by Lengths/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,33 @@ Triangles table:

## 解法

### 方法一
### 方法一:使用 CASE WHEN 语句

我们可以使用 `CASE WHEN` 语句来判断三角形的类型。

首先,我们需要判断三个边是否能够构成一个三角形。如果不能,我们返回 `Not A Triangle`。

然后,我们判断三个边的长度是否相等。如果相等,我们返回 `Equilateral`。

接着,我们判断是否有两个边的长度相等。如果有,我们返回 `Isosceles`。

否则,说明三个边的长度都不相等,我们返回 `Scalene`。

<!-- tabs:start -->

```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;
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

<!-- tabs:start -->

```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;
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
15 changes: 13 additions & 2 deletions solution/3000-3099/3054.Binary Tree Nodes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,23 @@ Tree table:

## 解法

### 方法一
### 方法一:左连接

如果一个节点的父节点为空,则它是根节点;如果一个节点不是任何节点的父节点,则它是叶子节点;否则它是内部节点。

因此,我们使用左连接来连接两次 `Tree` 表,连接条件是 `t1.N = t2.P`。那么如果 `t1.P` 为空,则 `t1.N` 是根节点;如果 `t2.P` 为空,则 `t1.N` 是叶子节点;否则 `t1.N` 是内部节点。

<!-- tabs:start -->

```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;
```

<!-- tabs:end -->
Expand Down
15 changes: 13 additions & 2 deletions solution/3000-3099/3054.Binary Tree Nodes/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- tabs:start -->

```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;
```

<!-- tabs:end -->
Expand Down
8 changes: 8 additions & 0 deletions solution/3000-3099/3054.Binary Tree Nodes/Solution.sql
Original file line number Diff line number Diff line change
@@ -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;
20 changes: 18 additions & 2 deletions solution/3000-3099/3055.Top Percentile Fraud/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,28 @@ Output table is ordered by state in ascending order, fraud score in descending o

## 解法

### 方法一
### 方法一:使用窗口函数

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

<!-- tabs:start -->

```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;
```

<!-- tabs:end -->
Expand Down
20 changes: 18 additions & 2 deletions solution/3000-3099/3055.Top Percentile Fraud/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- tabs:start -->

```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;
```

<!-- tabs:end -->
Expand Down
15 changes: 15 additions & 0 deletions solution/3000-3099/3055.Top Percentile Fraud/Solution.sql
Original file line number Diff line number Diff line change
@@ -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;
14 changes: 12 additions & 2 deletions solution/3000-3099/3056.Snaps Analysis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,22 @@ All percentages in output table rounded to the two decimal places.

## 解法

### 方法一
### 方法一:等值连接 + 分组求和

我们可以通过等值连接,将 `Activities` 表和 `Age` 表按照 `user_id` 进行连接,然后再按照 `age_bucket` 进行分组,最后计算每个年龄段的发送和打开的百分比。

<!-- tabs:start -->

```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 1;
```

<!-- tabs:end -->
Expand Down
14 changes: 12 additions & 2 deletions solution/3000-3099/3056.Snaps Analysis/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- tabs:start -->

```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 1;
```

<!-- tabs:end -->
Expand Down
9 changes: 9 additions & 0 deletions solution/3000-3099/3056.Snaps Analysis/Solution.sql
Original file line number Diff line number Diff line change
@@ -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 1;