Skip to content

Commit 725e8f9

Browse files
authored
feat: update solutions to lc problems (#1784)
* No.0511.Game Play Analysis I * No.0586.Customer Placing the Largest Number of Orders * No.1212.Team Scores in Football Tournament * No.1440.Evaluate Boolean Expression * No.1571.Warehouse Manager * No.1741.Find Total Time Spent by Each Employee * No.1742.Maximum Number of Balls in a Box * No.1890.The Latest Login in 2020 * No.2512.Reward Top K Students
1 parent e1a0ba6 commit 725e8f9

File tree

26 files changed

+263
-133
lines changed

26 files changed

+263
-133
lines changed

solution/0500-0599/0511.Game Play Analysis I/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Result 表:
5454

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

57-
**方法一:GROUP BY**
57+
**方法一:分组求最小值**
5858

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

@@ -66,7 +66,7 @@ Result 表:
6666
# Write your MySQL query statement below
6767
SELECT player_id, min(event_date) AS first_login
6868
FROM Activity
69-
GROUP BY player_id;
69+
GROUP BY 1;
7070
```
7171

7272
<!-- tabs:end -->

solution/0500-0599/0511.Game Play Analysis I/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ Activity table:
5555

5656
## Solutions
5757

58+
**Solution 1: Group By + Min Function**
59+
60+
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.
61+
5862
<!-- tabs:start -->
5963

6064
### **SQL**
@@ -63,7 +67,7 @@ Activity table:
6367
# Write your MySQL query statement below
6468
SELECT player_id, min(event_date) AS first_login
6569
FROM Activity
66-
GROUP BY player_id;
70+
GROUP BY 1;
6771
```
6872

6973
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Write your MySQL query statement below
22
SELECT player_id, min(event_date) AS first_login
33
FROM Activity
4-
GROUP BY player_id;
4+
GROUP BY 1;

solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ customer_number 为 '3' 的顾客有两个订单,比顾客 '1' 或者 '2' 都
6161

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

64+
**方法一:分组 + 排序**
65+
66+
我们可以使用 `GROUP BY` 将数据按照 `customer_number` 进行分组,然后按照 `count(1)` 进行降序排序,最后取第一条记录的 `customer_number` 即可。
67+
6468
<!-- tabs:start -->
6569

6670
### **SQL**

solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README_EN.md

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ So the result is customer_number 3.
5555

5656
## Solutions
5757

58+
**Solution 1: Group By + Sorting**
59+
60+
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.
61+
5862
<!-- tabs:start -->
5963

6064
### **SQL**

solution/1200-1299/1212.Team Scores in Football Tournament/README.md

+24-12
Original file line numberDiff line numberDiff line change
@@ -95,30 +95,42 @@ Teams </code>table:
9595

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

98+
**方法一:左连接 + 分组 + CASE 表达式**
99+
100+
我们可以通过左连接,将 `Teams` 表和 `Matches` 表连接起来,连接的条件为 `team_id = host_team OR team_id = guest_team`,这样就可以得到每个球队的所有比赛信息。
101+
102+
接下来,我们按照 `team_id` 分组,然后使用 `CASE` 表达式计算每个球队的积分,计算规则如下:
103+
104+
- 如果球队是主队,且主队进球数大于客队进球数,则积分加 $3$ 分;
105+
- 如果球队是客队,且客队进球数大于主队进球数,则积分加 $3$ 分;
106+
- 如果主队和客队进球数相同,则积分加 $1$ 分;
107+
108+
最后,我们按照积分降序排序,如果积分相同,则按照 `team_id` 升序排序。
109+
98110
<!-- tabs:start -->
99111

100112
### **SQL**
101113

102114
```sql
103115
# Write your MySQL query statement below
104116
SELECT
105-
t.team_id,
106-
t.team_name,
107-
SUM(
117+
team_id,
118+
team_name,
119+
sum(
108120
CASE
109-
WHEN t.team_id = m.host_team
110-
AND m.host_goals > m.guest_goals THEN 3
111-
WHEN m.host_goals = m.guest_goals THEN 1
112-
WHEN t.team_id = m.guest_team
113-
AND m.guest_goals > m.host_goals THEN 3
121+
WHEN team_id = host_team
122+
AND host_goals > guest_goals THEN 3
123+
WHEN team_id = guest_team
124+
AND guest_goals > host_goals THEN 3
125+
WHEN host_goals = guest_goals THEN 1
114126
ELSE 0
115127
END
116128
) AS num_points
117129
FROM
118-
Teams AS t
119-
LEFT JOIN Matches AS m ON t.team_id = m.host_team OR t.team_id = m.guest_team
120-
GROUP BY t.team_id
121-
ORDER BY num_points DESC, team_id;
130+
Teams
131+
LEFT JOIN Matches ON team_id = host_team OR team_id = guest_team
132+
GROUP BY 1
133+
ORDER BY 3 DESC, 1;
122134
```
123135

124136
<!-- tabs:end -->

solution/1200-1299/1212.Team Scores in Football Tournament/README_EN.md

+24-12
Original file line numberDiff line numberDiff line change
@@ -90,30 +90,42 @@ Matches table:
9090

9191
## Solutions
9292

93+
**Solution 1: Left Join + Group By + Case Expression**
94+
95+
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.
96+
97+
Next, we group by `team_id` and use a `CASE` expression to calculate the points for each team according to the following rules:
98+
99+
- If the team is the host team and has more goals than the guest team, add $3$ points to the team's score.
100+
- If the team is the guest team and has more goals than the host team, add $3$ points to the team's score.
101+
- If the host team and the guest team have the same number of goals, add $1$ point to the team's score.
102+
103+
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.
104+
93105
<!-- tabs:start -->
94106

95107
### **SQL**
96108

97109
```sql
98110
# Write your MySQL query statement below
99111
SELECT
100-
t.team_id,
101-
t.team_name,
102-
SUM(
112+
team_id,
113+
team_name,
114+
sum(
103115
CASE
104-
WHEN t.team_id = m.host_team
105-
AND m.host_goals > m.guest_goals THEN 3
106-
WHEN m.host_goals = m.guest_goals THEN 1
107-
WHEN t.team_id = m.guest_team
108-
AND m.guest_goals > m.host_goals THEN 3
116+
WHEN team_id = host_team
117+
AND host_goals > guest_goals THEN 3
118+
WHEN team_id = guest_team
119+
AND guest_goals > host_goals THEN 3
120+
WHEN host_goals = guest_goals THEN 1
109121
ELSE 0
110122
END
111123
) AS num_points
112124
FROM
113-
Teams AS t
114-
LEFT JOIN Matches AS m ON t.team_id = m.host_team OR t.team_id = m.guest_team
115-
GROUP BY t.team_id
116-
ORDER BY num_points DESC, team_id;
125+
Teams
126+
LEFT JOIN Matches ON team_id = host_team OR team_id = guest_team
127+
GROUP BY 1
128+
ORDER BY 3 DESC, 1;
117129
```
118130

119131
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# Write your MySQL query statement below
22
SELECT
3-
t.team_id,
4-
t.team_name,
5-
SUM(
3+
team_id,
4+
team_name,
5+
sum(
66
CASE
7-
WHEN t.team_id = m.host_team
8-
AND m.host_goals > m.guest_goals THEN 3
9-
WHEN m.host_goals = m.guest_goals THEN 1
10-
WHEN t.team_id = m.guest_team
11-
AND m.guest_goals > m.host_goals THEN 3
7+
WHEN team_id = host_team
8+
AND host_goals > guest_goals THEN 3
9+
WHEN team_id = guest_team
10+
AND guest_goals > host_goals THEN 3
11+
WHEN host_goals = guest_goals THEN 1
1212
ELSE 0
1313
END
1414
) AS num_points
1515
FROM
16-
Teams AS t
17-
LEFT JOIN Matches AS m ON t.team_id = m.host_team OR t.team_id = m.guest_team
18-
GROUP BY t.team_id
19-
ORDER BY num_points DESC, team_id;
16+
Teams
17+
LEFT JOIN Matches ON team_id = host_team OR team_id = guest_team
18+
GROUP BY 1
19+
ORDER BY 3 DESC, 1;

solution/1400-1499/1440.Evaluate Boolean Expression/README.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ Expressions 表:
9090

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

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

9599
### **SQL**
@@ -102,16 +106,16 @@ SELECT
102106
right_operand,
103107
CASE
104108
WHEN (
105-
(e.operator = '=' AND v1.value = v2.value)
106-
OR (e.operator = '>' AND v1.value > v2.value)
107-
OR (e.operator = '<' AND v1.value < v2.value)
109+
(operator = '=' AND v1.value = v2.value)
110+
OR (operator = '>' AND v1.value > v2.value)
111+
OR (operator = '<' AND v1.value < v2.value)
108112
) THEN 'true'
109113
ELSE 'false'
110114
END AS value
111115
FROM
112116
Expressions AS e
113-
LEFT JOIN Variables AS v1 ON e.left_operand = v1.name
114-
LEFT JOIN Variables AS v2 ON e.right_operand = v2.name;
117+
JOIN Variables AS v1 ON e.left_operand = v1.name
118+
JOIN Variables AS v2 ON e.right_operand = v2.name;
115119
```
116120

117121
<!-- tabs:end -->

solution/1400-1499/1440.Evaluate Boolean Expression/README_EN.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ As shown, you need to find the value of each boolean expression in the table usi
8383

8484
## Solutions
8585

86+
**Solution 1: Equi-Join + CASE Expression**
87+
88+
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`.
89+
8690
<!-- tabs:start -->
8791

8892
### **SQL**
@@ -95,16 +99,16 @@ SELECT
9599
right_operand,
96100
CASE
97101
WHEN (
98-
(e.operator = '=' AND v1.value = v2.value)
99-
OR (e.operator = '>' AND v1.value > v2.value)
100-
OR (e.operator = '<' AND v1.value < v2.value)
102+
(operator = '=' AND v1.value = v2.value)
103+
OR (operator = '>' AND v1.value > v2.value)
104+
OR (operator = '<' AND v1.value < v2.value)
101105
) THEN 'true'
102106
ELSE 'false'
103107
END AS value
104108
FROM
105109
Expressions AS e
106-
LEFT JOIN Variables AS v1 ON e.left_operand = v1.name
107-
LEFT JOIN Variables AS v2 ON e.right_operand = v2.name;
110+
JOIN Variables AS v1 ON e.left_operand = v1.name
111+
JOIN Variables AS v2 ON e.right_operand = v2.name;
108112
```
109113

110114
<!-- tabs:end -->

solution/1400-1499/1440.Evaluate Boolean Expression/Solution.sql

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ SELECT
55
right_operand,
66
CASE
77
WHEN (
8-
(e.operator = '=' AND v1.value = v2.value)
9-
OR (e.operator = '>' AND v1.value > v2.value)
10-
OR (e.operator = '<' AND v1.value < v2.value)
8+
(operator = '=' AND v1.value = v2.value)
9+
OR (operator = '>' AND v1.value > v2.value)
10+
OR (operator = '<' AND v1.value < v2.value)
1111
) THEN 'true'
1212
ELSE 'false'
1313
END AS value
1414
FROM
1515
Expressions AS e
16-
LEFT JOIN Variables AS v1 ON e.left_operand = v1.name
17-
LEFT JOIN Variables AS v2 ON e.right_operand = v2.name;
16+
JOIN Variables AS v1 ON e.left_operand = v1.name
17+
JOIN Variables AS v2 ON e.right_operand = v2.name;

solution/1500-1599/1571.Warehouse Manager/README.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ Id为4的商品(LC-T-Shirt)的存货量为 4x10x20 = 800
9696

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

99+
**方法一:等值连接 + 分组求和**
100+
101+
我们可以使用等值连接将 `Warehouse` 表和 `Products` 表按照 `product_id` 进行连接,并按照仓库名称进行分组,然后使用 `SUM` 函数计算每个仓库的存货量。
102+
99103
<!-- tabs:start -->
100104

101105
### **SQL**
@@ -104,11 +108,11 @@ Id为4的商品(LC-T-Shirt)的存货量为 4x10x20 = 800
104108
# Write your MySQL query statement below
105109
SELECT
106110
name AS warehouse_name,
107-
SUM(units * Width * Length * Height) AS volume
111+
sum(width * length * height * units) AS volume
108112
FROM
109-
Warehouse AS w
110-
JOIN Products AS p ON w.product_id = p.product_id
111-
GROUP BY name;
113+
Warehouse
114+
JOIN Products USING (product_id)
115+
GROUP BY 1;
112116
```
113117

114118
<!-- tabs:end -->

solution/1500-1599/1571.Warehouse Manager/README_EN.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ LCHouse3: 1 unit of LC-T-Shirt.
9292

9393
## Solutions
9494

95+
**Solution 1: Inner Join + Group By + Sum Function**
96+
97+
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.
98+
9599
<!-- tabs:start -->
96100

97101
### **SQL**
@@ -100,11 +104,11 @@ LCHouse3: 1 unit of LC-T-Shirt.
100104
# Write your MySQL query statement below
101105
SELECT
102106
name AS warehouse_name,
103-
SUM(units * Width * Length * Height) AS volume
107+
sum(width * length * height * units) AS volume
104108
FROM
105-
Warehouse AS w
106-
JOIN Products AS p ON w.product_id = p.product_id
107-
GROUP BY name;
109+
Warehouse
110+
JOIN Products USING (product_id)
111+
GROUP BY 1;
108112
```
109113

110114
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Write your MySQL query statement below
22
SELECT
33
name AS warehouse_name,
4-
SUM(units * Width * Length * Height) AS volume
4+
sum(width * length * height * units) AS volume
55
FROM
6-
Warehouse AS w
7-
JOIN Products AS p ON w.product_id = p.product_id
8-
GROUP BY name;
6+
Warehouse
7+
JOIN Products USING (product_id)
8+
GROUP BY 1;

solution/1700-1799/1741.Find Total Time Spent by Each Employee/README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,19 @@ Employees table:
6565

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

68+
**方法一:分组求和**
69+
70+
我们可以先按照 `emp_id``event_day` 进行分组,然后计算每个分组的总时间。总时间等于每个分组的 `out_time` 减去 `in_time` 的和。
71+
6872
<!-- tabs:start -->
6973

7074
### **SQL**
7175

7276
```sql
73-
SELECT
74-
event_day AS day,
75-
emp_id,
76-
SUM(out_time - in_time) AS total_time
77+
# Write your MySQL query statement below
78+
SELECT event_day AS day, emp_id, sum(out_time - in_time) AS total_time
7779
FROM Employees
78-
GROUP BY emp_id, event_day;
80+
GROUP BY 1, 2;
7981
```
8082

8183
<!-- tabs:end -->

0 commit comments

Comments
 (0)