Skip to content
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

feat: update solutions to lc problems #1784

Merged
merged 1 commit into from
Oct 11, 2023
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
4 changes: 2 additions & 2 deletions solution/0500-0599/0511.Game Play Analysis I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Result 表:

<!-- 这里可写通用的实现逻辑 -->

**方法一:GROUP BY**
**方法一:分组求最小值**

我们可以用 `GROUP BY` 对 `player_id` 进行分组,然后取每一组中最小的 `event_date` 作为玩家第一次登录平台的日期。

Expand All @@ -66,7 +66,7 @@ Result 表:
# Write your MySQL query statement below
SELECT player_id, min(event_date) AS first_login
FROM Activity
GROUP BY player_id;
GROUP BY 1;
```

<!-- tabs:end -->
6 changes: 5 additions & 1 deletion solution/0500-0599/0511.Game Play Analysis I/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ Activity table:

## Solutions

**Solution 1: Group By + Min Function**

We can use `GROUP BY` to group the `player_id` and then take the minimum `event_date` in each group as the date when the player first logged into the platform.

<!-- tabs:start -->

### **SQL**
Expand All @@ -63,7 +67,7 @@ Activity table:
# Write your MySQL query statement below
SELECT player_id, min(event_date) AS first_login
FROM Activity
GROUP BY player_id;
GROUP BY 1;
```

<!-- tabs:end -->
2 changes: 1 addition & 1 deletion solution/0500-0599/0511.Game Play Analysis I/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Write your MySQL query statement below
SELECT player_id, min(event_date) AS first_login
FROM Activity
GROUP BY player_id;
GROUP BY 1;
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ customer_number 为 '3' 的顾客有两个订单,比顾客 '1' 或者 '2' 都

<!-- 这里可写通用的实现逻辑 -->

**方法一:分组 + 排序**

我们可以使用 `GROUP BY` 将数据按照 `customer_number` 进行分组,然后按照 `count(1)` 进行降序排序,最后取第一条记录的 `customer_number` 即可。

<!-- tabs:start -->

### **SQL**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ So the result is customer_number 3.

## Solutions

**Solution 1: Group By + Sorting**

We can use `GROUP BY` to group the data by `customer_number`, and then sort the groups in descending order by `count(1)`. Finally, we can take the `customer_number` of the first record as the result.

<!-- tabs:start -->

### **SQL**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,30 +95,42 @@ Teams </code>table:

<!-- 这里可写通用的实现逻辑 -->

**方法一:左连接 + 分组 + CASE 表达式**

我们可以通过左连接,将 `Teams` 表和 `Matches` 表连接起来,连接的条件为 `team_id = host_team OR team_id = guest_team`,这样就可以得到每个球队的所有比赛信息。

接下来,我们按照 `team_id` 分组,然后使用 `CASE` 表达式计算每个球队的积分,计算规则如下:

- 如果球队是主队,且主队进球数大于客队进球数,则积分加 $3$ 分;
- 如果球队是客队,且客队进球数大于主队进球数,则积分加 $3$ 分;
- 如果主队和客队进球数相同,则积分加 $1$ 分;

最后,我们按照积分降序排序,如果积分相同,则按照 `team_id` 升序排序。

<!-- tabs:start -->

### **SQL**

```sql
# Write your MySQL query statement below
SELECT
t.team_id,
t.team_name,
SUM(
team_id,
team_name,
sum(
CASE
WHEN t.team_id = m.host_team
AND m.host_goals > m.guest_goals THEN 3
WHEN m.host_goals = m.guest_goals THEN 1
WHEN t.team_id = m.guest_team
AND m.guest_goals > m.host_goals THEN 3
WHEN team_id = host_team
AND host_goals > guest_goals THEN 3
WHEN team_id = guest_team
AND guest_goals > host_goals THEN 3
WHEN host_goals = guest_goals THEN 1
ELSE 0
END
) AS num_points
FROM
Teams AS t
LEFT JOIN Matches AS m ON t.team_id = m.host_team OR t.team_id = m.guest_team
GROUP BY t.team_id
ORDER BY num_points DESC, team_id;
Teams
LEFT JOIN Matches ON team_id = host_team OR team_id = guest_team
GROUP BY 1
ORDER BY 3 DESC, 1;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -90,30 +90,42 @@ Matches table:

## Solutions

**Solution 1: Left Join + Group By + Case Expression**

We can join the `Teams` table and the `Matches` table using a left join, where the join condition is `team_id = host_team OR team_id = guest_team`, to obtain all the match information for each team.

Next, we group by `team_id` and use a `CASE` expression to calculate the points for each team according to the following rules:

- If the team is the host team and has more goals than the guest team, add $3$ points to the team's score.
- If the team is the guest team and has more goals than the host team, add $3$ points to the team's score.
- If the host team and the guest team have the same number of goals, add $1$ point to the team's score.

Finally, we sort the result by points in descending order, and if the points are the same, we sort by `team_id` in ascending order.

<!-- tabs:start -->

### **SQL**

```sql
# Write your MySQL query statement below
SELECT
t.team_id,
t.team_name,
SUM(
team_id,
team_name,
sum(
CASE
WHEN t.team_id = m.host_team
AND m.host_goals > m.guest_goals THEN 3
WHEN m.host_goals = m.guest_goals THEN 1
WHEN t.team_id = m.guest_team
AND m.guest_goals > m.host_goals THEN 3
WHEN team_id = host_team
AND host_goals > guest_goals THEN 3
WHEN team_id = guest_team
AND guest_goals > host_goals THEN 3
WHEN host_goals = guest_goals THEN 1
ELSE 0
END
) AS num_points
FROM
Teams AS t
LEFT JOIN Matches AS m ON t.team_id = m.host_team OR t.team_id = m.guest_team
GROUP BY t.team_id
ORDER BY num_points DESC, team_id;
Teams
LEFT JOIN Matches ON team_id = host_team OR team_id = guest_team
GROUP BY 1
ORDER BY 3 DESC, 1;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Write your MySQL query statement below
SELECT
t.team_id,
t.team_name,
SUM(
team_id,
team_name,
sum(
CASE
WHEN t.team_id = m.host_team
AND m.host_goals > m.guest_goals THEN 3
WHEN m.host_goals = m.guest_goals THEN 1
WHEN t.team_id = m.guest_team
AND m.guest_goals > m.host_goals THEN 3
WHEN team_id = host_team
AND host_goals > guest_goals THEN 3
WHEN team_id = guest_team
AND guest_goals > host_goals THEN 3
WHEN host_goals = guest_goals THEN 1
ELSE 0
END
) AS num_points
FROM
Teams AS t
LEFT JOIN Matches AS m ON t.team_id = m.host_team OR t.team_id = m.guest_team
GROUP BY t.team_id
ORDER BY num_points DESC, team_id;
Teams
LEFT JOIN Matches ON team_id = host_team OR team_id = guest_team
GROUP BY 1
ORDER BY 3 DESC, 1;
14 changes: 9 additions & 5 deletions solution/1400-1499/1440.Evaluate Boolean Expression/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ Expressions 表:

<!-- 这里可写通用的实现逻辑 -->

**方法一:等值连接 + CASE 表达式**

我们可以通过等值连接,将 `Expressions` 表中的每一行与 `Variables` 表中的两行进行关联,关联的条件是 `left_operand = name` 和 `right_operand = name`,然后通过 `CASE` 表达式来判断布尔表达式的值。如果 `operator` 为 `=`,则判断两个值是否相等;如果 `operator` 为 `>`,则判断左值是否大于右值;如果 `operator` 为 `<`,则判断左值是否小于右值。若是,那么布尔表达式的值为 `true`,否则为 `false`。

<!-- tabs:start -->

### **SQL**
Expand All @@ -102,16 +106,16 @@ SELECT
right_operand,
CASE
WHEN (
(e.operator = '=' AND v1.value = v2.value)
OR (e.operator = '>' AND v1.value > v2.value)
OR (e.operator = '<' AND v1.value < v2.value)
(operator = '=' AND v1.value = v2.value)
OR (operator = '>' AND v1.value > v2.value)
OR (operator = '<' AND v1.value < v2.value)
) THEN 'true'
ELSE 'false'
END AS value
FROM
Expressions AS e
LEFT JOIN Variables AS v1 ON e.left_operand = v1.name
LEFT JOIN Variables AS v2 ON e.right_operand = v2.name;
JOIN Variables AS v1 ON e.left_operand = v1.name
JOIN Variables AS v2 ON e.right_operand = v2.name;
```

<!-- tabs:end -->
14 changes: 9 additions & 5 deletions solution/1400-1499/1440.Evaluate Boolean Expression/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ As shown, you need to find the value of each boolean expression in the table usi

## Solutions

**Solution 1: Equi-Join + CASE Expression**

We can associate each row in the `Expressions` table with two rows in the `Variables` table using an equi-join, where the conditions for the association are `left_operand = name` and `right_operand = name`. Then, we can use a `CASE` expression to determine the value of the boolean expression. If the `operator` is `=`, we check if the two values are equal. If the `operator` is `>`, we check if the left value is greater than the right value. If the `operator` is `<`, we check if the left value is less than the right value. If the condition is true, the boolean expression evaluates to `true`, otherwise it evaluates to `false`.

<!-- tabs:start -->

### **SQL**
Expand All @@ -95,16 +99,16 @@ SELECT
right_operand,
CASE
WHEN (
(e.operator = '=' AND v1.value = v2.value)
OR (e.operator = '>' AND v1.value > v2.value)
OR (e.operator = '<' AND v1.value < v2.value)
(operator = '=' AND v1.value = v2.value)
OR (operator = '>' AND v1.value > v2.value)
OR (operator = '<' AND v1.value < v2.value)
) THEN 'true'
ELSE 'false'
END AS value
FROM
Expressions AS e
LEFT JOIN Variables AS v1 ON e.left_operand = v1.name
LEFT JOIN Variables AS v2 ON e.right_operand = v2.name;
JOIN Variables AS v1 ON e.left_operand = v1.name
JOIN Variables AS v2 ON e.right_operand = v2.name;
```

<!-- tabs:end -->
10 changes: 5 additions & 5 deletions solution/1400-1499/1440.Evaluate Boolean Expression/Solution.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ SELECT
right_operand,
CASE
WHEN (
(e.operator = '=' AND v1.value = v2.value)
OR (e.operator = '>' AND v1.value > v2.value)
OR (e.operator = '<' AND v1.value < v2.value)
(operator = '=' AND v1.value = v2.value)
OR (operator = '>' AND v1.value > v2.value)
OR (operator = '<' AND v1.value < v2.value)
) THEN 'true'
ELSE 'false'
END AS value
FROM
Expressions AS e
LEFT JOIN Variables AS v1 ON e.left_operand = v1.name
LEFT JOIN Variables AS v2 ON e.right_operand = v2.name;
JOIN Variables AS v1 ON e.left_operand = v1.name
JOIN Variables AS v2 ON e.right_operand = v2.name;
12 changes: 8 additions & 4 deletions solution/1500-1599/1571.Warehouse Manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ Id为4的商品(LC-T-Shirt)的存货量为 4x10x20 = 800

<!-- 这里可写通用的实现逻辑 -->

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

我们可以使用等值连接将 `Warehouse` 表和 `Products` 表按照 `product_id` 进行连接,并按照仓库名称进行分组,然后使用 `SUM` 函数计算每个仓库的存货量。

<!-- tabs:start -->

### **SQL**
Expand All @@ -104,11 +108,11 @@ Id为4的商品(LC-T-Shirt)的存货量为 4x10x20 = 800
# Write your MySQL query statement below
SELECT
name AS warehouse_name,
SUM(units * Width * Length * Height) AS volume
sum(width * length * height * units) AS volume
FROM
Warehouse AS w
JOIN Products AS p ON w.product_id = p.product_id
GROUP BY name;
Warehouse
JOIN Products USING (product_id)
GROUP BY 1;
```

<!-- tabs:end -->
12 changes: 8 additions & 4 deletions solution/1500-1599/1571.Warehouse Manager/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ LCHouse3: 1 unit of LC-T-Shirt.

## Solutions

**Solution 1: Inner Join + Group By + Sum Function**

We can use an inner join to join the `Warehouse` table and the `Products` table on the condition of `product_id`, and then group by warehouse name to calculate the inventory of each warehouse using the `SUM` function.

<!-- tabs:start -->

### **SQL**
Expand All @@ -100,11 +104,11 @@ LCHouse3: 1 unit of LC-T-Shirt.
# Write your MySQL query statement below
SELECT
name AS warehouse_name,
SUM(units * Width * Length * Height) AS volume
sum(width * length * height * units) AS volume
FROM
Warehouse AS w
JOIN Products AS p ON w.product_id = p.product_id
GROUP BY name;
Warehouse
JOIN Products USING (product_id)
GROUP BY 1;
```

<!-- tabs:end -->
8 changes: 4 additions & 4 deletions solution/1500-1599/1571.Warehouse Manager/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Write your MySQL query statement below
SELECT
name AS warehouse_name,
SUM(units * Width * Length * Height) AS volume
sum(width * length * height * units) AS volume
FROM
Warehouse AS w
JOIN Products AS p ON w.product_id = p.product_id
GROUP BY name;
Warehouse
JOIN Products USING (product_id)
GROUP BY 1;
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,19 @@ Employees table:

<!-- 这里可写通用的实现逻辑 -->

**方法一:分组求和**

我们可以先按照 `emp_id` 和 `event_day` 进行分组,然后计算每个分组的总时间。总时间等于每个分组的 `out_time` 减去 `in_time` 的和。

<!-- tabs:start -->

### **SQL**

```sql
SELECT
event_day AS day,
emp_id,
SUM(out_time - in_time) AS total_time
# Write your MySQL query statement below
SELECT event_day AS day, emp_id, sum(out_time - in_time) AS total_time
FROM Employees
GROUP BY emp_id, event_day;
GROUP BY 1, 2;
```

<!-- tabs:end -->
Loading