Skip to content

Commit 102e2c3

Browse files
authored
feat: update solutions to lc problems: No.0534,0608,1264,1965 (#1791)
* No.0534.Game Play Analysis III * No.0608.Tree Node * No.1264.Page Recommendations * No.1965.Employees With Missing Information
1 parent 168ef90 commit 102e2c3

File tree

9 files changed

+144
-41
lines changed

9 files changed

+144
-41
lines changed

solution/0500-0599/0534.Game Play Analysis III/README.md

+31-2
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,13 @@ Activity table:
6666

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

69-
**方法一:SUM() OVER() 窗口函数**
69+
**方法一:使用窗口函数**
7070

71-
我们可以使用 `SUM() OVER()` 窗口函数来计算每个玩家到目前为止玩了多少游戏。在 `OVER()` 子句中,我们使用 `PARTITION BY` 子句将玩家分组,然后使用 `ORDER BY` 子句按日期排序。
71+
我们可以使用窗口函数 `SUM() OVER()`,按照 `player_id` 分组,按照 `event_date` 排序,计算每个用户截止到当前日期的游戏总数。
72+
73+
**方法二:使用自连接 + 分组**
74+
75+
我们也可以使用自连接,将 `Activity` 表自连接,连接条件为 `t1.player_id = t2.player_id AND t1.event_date >= t2.event_date`,然后按照 `t1.player_id``t1.event_date` 分组,累计 `t2.games_played`,得到每个用户截止到当前日期的游戏总数。
7276

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

@@ -86,4 +90,29 @@ SELECT
8690
FROM Activity;
8791
```
8892

93+
```sql
94+
# Write your MySQL query statement below
95+
SELECT
96+
t1.player_id,
97+
t1.event_date,
98+
sum(t2.games_played) AS games_played_so_far
99+
FROM
100+
Activity AS t1,
101+
Activity AS t2
102+
WHERE t1.player_id = t2.player_id AND t1.event_date >= t2.event_date
103+
GROUP BY 1, 2;
104+
```
105+
106+
```sql
107+
# Write your MySQL query statement below
108+
SELECT
109+
t1.player_id,
110+
t1.event_date,
111+
sum(t2.games_played) AS games_played_so_far
112+
FROM
113+
Activity AS t1
114+
CROSS JOIN Activity AS t2 ON t1.player_id = t2.player_id AND t1.event_date >= t2.event_date
115+
GROUP BY 1, 2;
116+
```
117+
89118
<!-- tabs:end -->

solution/0500-0599/0534.Game Play Analysis III/README_EN.md

+33
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ Note that for each player we only care about the days when the player logged in.
6161

6262
## Solutions
6363

64+
**Solution 1: Window Function**
65+
66+
We can use the window function `SUM() OVER()` to group by `player_id`, sort by `event_date`, and calculate the total number of games played by each user up to the current date.
67+
68+
**Solution 2: Self-Join + Group By**
69+
70+
We can also use a self-join to join the `Activity` table with itself on the condition of `t1.player_id = t2.player_id AND t1.event_date >= t2.event_date`, and then group by `t1.player_id` and `t1.event_date`, and calculate the cumulative sum of `t2.games_played`. This will give us the total number of games played by each user up to the current date.
71+
6472
<!-- tabs:start -->
6573

6674
### **SQL**
@@ -77,4 +85,29 @@ SELECT
7785
FROM Activity;
7886
```
7987

88+
```sql
89+
# Write your MySQL query statement below
90+
SELECT
91+
t1.player_id,
92+
t1.event_date,
93+
sum(t2.games_played) AS games_played_so_far
94+
FROM
95+
Activity AS t1,
96+
Activity AS t2
97+
WHERE t1.player_id = t2.player_id AND t1.event_date >= t2.event_date
98+
GROUP BY 1, 2;
99+
```
100+
101+
```sql
102+
# Write your MySQL query statement below
103+
SELECT
104+
t1.player_id,
105+
t1.event_date,
106+
sum(t2.games_played) AS games_played_so_far
107+
FROM
108+
Activity AS t1
109+
CROSS JOIN Activity AS t2 ON t1.player_id = t2.player_id AND t1.event_date >= t2.event_date
110+
GROUP BY 1, 2;
111+
```
112+
80113
<!-- tabs:end -->

solution/0600-0699/0608.Tree Node/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ Tree table:
9191

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

94+
**方法一:条件判断 + 子查询**
95+
96+
我们可以使用 `CASE WHEN` 条件判断语句来判断每个节点的类型,具体地:
97+
98+
- 如果一个节点的 `p_id``NULL`,则该节点为根节点;
99+
- 否则,如果一个节点是另一个节点的父节点(这里我们使用子查询来判断),则该节点为内部节点;
100+
- 否则,该节点为叶子节点。
101+
94102
<!-- tabs:start -->
95103

96104
### **SQL**

solution/0600-0699/0608.Tree Node/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ Tree table:
8686

8787
## Solutions
8888

89+
**Solution 1: Conditional Statements + Subquery**
90+
91+
We can use the `CASE WHEN` conditional statement to determine the type of each node as follows:
92+
93+
- If a node's `p_id` is `NULL`, then it is a root node.
94+
- Otherwise, if a node is the parent node of another node (we use a subquery to determine this), then it is an internal node.
95+
- Otherwise, it is a leaf node.
96+
8997
<!-- tabs:start -->
9098

9199
### **SQL**

solution/1200-1299/1264.Page Recommendations/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,29 @@ Likes table:
9696

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

99+
**方法一:合并 + 等值连接 + 子查询**
100+
101+
我们先查出所有与 `user_id = 1` 的用户是朋友的用户,记录在 `T` 表中,然后再查出所有在 `T` 表中的用户喜欢的页面,最后排除掉 `user_id = 1` 喜欢的页面即可。
102+
99103
<!-- tabs:start -->
100104

101105
### **SQL**
102106

107+
```sql
108+
# Write your MySQL query statement below
109+
WITH
110+
T AS (
111+
SELECT user1_id AS user_id FROM Friendship WHERE user2_id = 1
112+
UNION
113+
SELECT user2_id AS user_id FROM Friendship WHERE user1_id = 1
114+
)
115+
SELECT DISTINCT page_id AS recommended_page
116+
FROM
117+
T
118+
JOIN Likes USING (user_id)
119+
WHERE page_id NOT IN (SELECT page_id FROM Likes WHERE user_id = 1);
120+
```
121+
103122
```sql
104123
# Write your MySQL query statement below
105124
SELECT DISTINCT page_id AS recommended_page

solution/1200-1299/1264.Page Recommendations/README_EN.md

+19
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,29 @@ Page 88 is not suggested because user 1 already likes it.
9090

9191
## Solutions
9292

93+
**Solution 1: Union + Equi-Join + Subquery**
94+
95+
First, we query all users who are friends with `user_id = 1` and record them in the `T` table. Then, we query all pages that users in the `T` table like, and finally exclude the pages that `user_id = 1` likes.
96+
9397
<!-- tabs:start -->
9498

9599
### **SQL**
96100

101+
```sql
102+
# Write your MySQL query statement below
103+
WITH
104+
T AS (
105+
SELECT user1_id AS user_id FROM Friendship WHERE user2_id = 1
106+
UNION
107+
SELECT user2_id AS user_id FROM Friendship WHERE user1_id = 1
108+
)
109+
SELECT DISTINCT page_id AS recommended_page
110+
FROM
111+
T
112+
JOIN Likes USING (user_id)
113+
WHERE page_id NOT IN (SELECT page_id FROM Likes WHERE user_id = 1);
114+
```
115+
97116
```sql
98117
# Write your MySQL query statement below
99118
SELECT DISTINCT page_id AS recommended_page

solution/1900-1999/1965.Employees With Missing Information/README.md

+10-13
Original file line numberDiff line numberDiff line change
@@ -83,29 +83,26 @@ Salaries table:
8383

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

86+
**方法一:子查询 + 合并**
87+
88+
我们可以先从 `Employees` 表中找出所有不在 `Salaries` 表中的 `employee_id`,再从 `Salaries` 表中找出所有不在 `Employees` 表中的 `employee_id`,最后将两个结果合并,然后按照 `employee_id` 排序即可。
89+
8690
<!-- tabs:start -->
8791

8892
### **SQL**
8993

9094
<!-- 这里可写当前语言的特殊实现逻辑 -->
9195

9296
```sql
97+
# Write your MySQL query statement below
9398
SELECT employee_id
94-
FROM Employees AS e
95-
WHERE
96-
e.employee_id NOT IN (
97-
SELECT employee_id
98-
FROM Salaries
99-
)
99+
FROM Employees
100+
WHERE employee_id NOT IN (SELECT employee_id FROM Salaries)
100101
UNION
101102
SELECT employee_id
102-
FROM Salaries AS s
103-
WHERE
104-
s.employee_id NOT IN (
105-
SELECT employee_id
106-
FROM Employees
107-
)
108-
ORDER BY employee_id;
103+
FROM Salaries
104+
WHERE employee_id NOT IN (SELECT employee_id FROM Employees)
105+
ORDER BY 1;
109106
```
110107

111108
<!-- tabs:end -->

solution/1900-1999/1965.Employees With Missing Information/README_EN.md

+10-13
Original file line numberDiff line numberDiff line change
@@ -81,27 +81,24 @@ The salary of employee 2 is missing.
8181

8282
## Solutions
8383

84+
**Solution 1: Subquery + Union**
85+
86+
We can first find all `employee_id` that are not in the `Salaries` table from the `Employees` table, and then find all `employee_id` that are not in the `Employees` table from the `Salaries` table. Finally, we can combine the two results using the `UNION` operator, and sort the result by `employee_id`.
87+
8488
<!-- tabs:start -->
8589

8690
### **SQL**
8791

8892
```sql
93+
# Write your MySQL query statement below
8994
SELECT employee_id
90-
FROM Employees AS e
91-
WHERE
92-
e.employee_id NOT IN (
93-
SELECT employee_id
94-
FROM Salaries
95-
)
95+
FROM Employees
96+
WHERE employee_id NOT IN (SELECT employee_id FROM Salaries)
9697
UNION
9798
SELECT employee_id
98-
FROM Salaries AS s
99-
WHERE
100-
s.employee_id NOT IN (
101-
SELECT employee_id
102-
FROM Employees
103-
)
104-
ORDER BY employee_id;
99+
FROM Salaries
100+
WHERE employee_id NOT IN (SELECT employee_id FROM Employees)
101+
ORDER BY 1;
105102
```
106103

107104
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1+
# Write your MySQL query statement below
12
SELECT employee_id
2-
FROM Employees AS e
3-
WHERE
4-
e.employee_id NOT IN (
5-
SELECT employee_id
6-
FROM Salaries
7-
)
3+
FROM Employees
4+
WHERE employee_id NOT IN (SELECT employee_id FROM Salaries)
85
UNION
96
SELECT employee_id
10-
FROM Salaries AS s
11-
WHERE
12-
s.employee_id NOT IN (
13-
SELECT employee_id
14-
FROM Employees
15-
)
16-
ORDER BY employee_id;
7+
FROM Salaries
8+
WHERE employee_id NOT IN (SELECT employee_id FROM Employees)
9+
ORDER BY 1;

0 commit comments

Comments
 (0)