Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 725e8f9

Browse files
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‎

Lines changed: 2 additions & 2 deletions
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‎

Lines changed: 5 additions & 1 deletion
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 -->
Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 4 additions & 0 deletions
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‎

Lines changed: 4 additions & 0 deletions
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‎

Lines changed: 24 additions & 12 deletions
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-
ANDm.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-
TeamsAS 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‎

Lines changed: 24 additions & 12 deletions
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-
ANDm.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-
TeamsAS 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 -->
Lines changed: 12 additions & 12 deletions
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-
ANDm.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-
TeamsAS 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‎

Lines changed: 9 additions & 5 deletions
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‎

Lines changed: 9 additions & 5 deletions
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 -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /