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

feat: update solutions to lc problems: No.0534,0608,1264,1965 #1791

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
yanglbme merged 2 commits into main from dev
Oct 12, 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
33 changes: 31 additions & 2 deletions solution/0500-0599/0534.Game Play Analysis III/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ Activity table:

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

**方法一:SUM() OVER() 窗口函数**
**方法一:使用窗口函数**

我们可以使用 `SUM() OVER()` 窗口函数来计算每个玩家到目前为止玩了多少游戏。在 `OVER()` 子句中,我们使用 `PARTITION BY` 子句将玩家分组,然后使用 `ORDER BY` 子句按日期排序。
我们可以使用窗口函数 `SUM() OVER()`,按照 `player_id` 分组,按照 `event_date` 排序,计算每个用户截止到当前日期的游戏总数。

**方法二:使用自连接 + 分组**

我们也可以使用自连接,将 `Activity` 表自连接,连接条件为 `t1.player_id = t2.player_id AND t1.event_date >= t2.event_date`,然后按照 `t1.player_id` 和 `t1.event_date` 分组,累计 `t2.games_played`,得到每个用户截止到当前日期的游戏总数。

<!-- tabs:start -->

Expand All @@ -86,4 +90,29 @@ SELECT
FROM Activity;
```

```sql
# Write your MySQL query statement below
SELECT
t1.player_id,
t1.event_date,
sum(t2.games_played) AS games_played_so_far
FROM
Activity AS t1,
Activity AS t2
WHERE t1.player_id = t2.player_id AND t1.event_date >= t2.event_date
GROUP BY 1, 2;
```

```sql
# Write your MySQL query statement below
SELECT
t1.player_id,
t1.event_date,
sum(t2.games_played) AS games_played_so_far
FROM
Activity AS t1
CROSS JOIN Activity AS t2 ON t1.player_id = t2.player_id AND t1.event_date >= t2.event_date
GROUP BY 1, 2;
```

<!-- tabs:end -->
33 changes: 33 additions & 0 deletions solution/0500-0599/0534.Game Play Analysis III/README_EN.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ Note that for each player we only care about the days when the player logged in.

## Solutions

**Solution 1: Window Function**

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.

**Solution 2: Self-Join + Group By**

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.

<!-- tabs:start -->

### **SQL**
Expand All @@ -77,4 +85,29 @@ SELECT
FROM Activity;
```

```sql
# Write your MySQL query statement below
SELECT
t1.player_id,
t1.event_date,
sum(t2.games_played) AS games_played_so_far
FROM
Activity AS t1,
Activity AS t2
WHERE t1.player_id = t2.player_id AND t1.event_date >= t2.event_date
GROUP BY 1, 2;
```

```sql
# Write your MySQL query statement below
SELECT
t1.player_id,
t1.event_date,
sum(t2.games_played) AS games_played_so_far
FROM
Activity AS t1
CROSS JOIN Activity AS t2 ON t1.player_id = t2.player_id AND t1.event_date >= t2.event_date
GROUP BY 1, 2;
```

<!-- tabs:end -->
8 changes: 8 additions & 0 deletions solution/0600-0699/0608.Tree Node/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ Tree table:

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

**方法一:条件判断 + 子查询**

我们可以使用 `CASE WHEN` 条件判断语句来判断每个节点的类型,具体地:

- 如果一个节点的 `p_id` 为 `NULL`,则该节点为根节点;
- 否则,如果一个节点是另一个节点的父节点(这里我们使用子查询来判断),则该节点为内部节点;
- 否则,该节点为叶子节点。

<!-- tabs:start -->

### **SQL**
Expand Down
8 changes: 8 additions & 0 deletions solution/0600-0699/0608.Tree Node/README_EN.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ Tree table:

## Solutions

**Solution 1: Conditional Statements + Subquery**

We can use the `CASE WHEN` conditional statement to determine the type of each node as follows:

- If a node's `p_id` is `NULL`, then it is a root node.
- Otherwise, if a node is the parent node of another node (we use a subquery to determine this), then it is an internal node.
- Otherwise, it is a leaf node.

<!-- tabs:start -->

### **SQL**
Expand Down
19 changes: 19 additions & 0 deletions solution/1200-1299/1264.Page Recommendations/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,29 @@ Likes table:

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

**方法一:合并 + 等值连接 + 子查询**

我们先查出所有与 `user_id = 1` 的用户是朋友的用户,记录在 `T` 表中,然后再查出所有在 `T` 表中的用户喜欢的页面,最后排除掉 `user_id = 1` 喜欢的页面即可。

<!-- tabs:start -->

### **SQL**

```sql
# Write your MySQL query statement below
WITH
T AS (
SELECT user1_id AS user_id FROM Friendship WHERE user2_id = 1
UNION
SELECT user2_id AS user_id FROM Friendship WHERE user1_id = 1
)
SELECT DISTINCT page_id AS recommended_page
FROM
T
JOIN Likes USING (user_id)
WHERE page_id NOT IN (SELECT page_id FROM Likes WHERE user_id = 1);
```

```sql
# Write your MySQL query statement below
SELECT DISTINCT page_id AS recommended_page
Expand Down
19 changes: 19 additions & 0 deletions solution/1200-1299/1264.Page Recommendations/README_EN.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,29 @@ Page 88 is not suggested because user 1 already likes it.

## Solutions

**Solution 1: Union + Equi-Join + Subquery**

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.

<!-- tabs:start -->

### **SQL**

```sql
# Write your MySQL query statement below
WITH
T AS (
SELECT user1_id AS user_id FROM Friendship WHERE user2_id = 1
UNION
SELECT user2_id AS user_id FROM Friendship WHERE user1_id = 1
)
SELECT DISTINCT page_id AS recommended_page
FROM
T
JOIN Likes USING (user_id)
WHERE page_id NOT IN (SELECT page_id FROM Likes WHERE user_id = 1);
```

```sql
# Write your MySQL query statement below
SELECT DISTINCT page_id AS recommended_page
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -83,29 +83,26 @@ Salaries table:

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

**方法一:子查询 + 合并**

我们可以先从 `Employees` 表中找出所有不在 `Salaries` 表中的 `employee_id`,再从 `Salaries` 表中找出所有不在 `Employees` 表中的 `employee_id`,最后将两个结果合并,然后按照 `employee_id` 排序即可。

<!-- tabs:start -->

### **SQL**

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

```sql
# Write your MySQL query statement below
SELECT employee_id
FROM Employees AS e
WHERE
e.employee_id NOT IN (
SELECT employee_id
FROM Salaries
)
FROM Employees
WHERE employee_id NOT IN (SELECT employee_id FROM Salaries)
UNION
SELECT employee_id
FROM Salaries AS s
WHERE
s.employee_id NOT IN (
SELECT employee_id
FROM Employees
)
ORDER BY employee_id;
FROM Salaries
WHERE employee_id NOT IN (SELECT employee_id FROM Employees)
ORDER BY 1;
```

<!-- tabs:end -->
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,24 @@ The salary of employee 2 is missing.

## Solutions

**Solution 1: Subquery + Union**

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

<!-- tabs:start -->

### **SQL**

```sql
# Write your MySQL query statement below
SELECT employee_id
FROM Employees AS e
WHERE
e.employee_id NOT IN (
SELECT employee_id
FROM Salaries
)
FROM Employees
WHERE employee_id NOT IN (SELECT employee_id FROM Salaries)
UNION
SELECT employee_id
FROM Salaries AS s
WHERE
s.employee_id NOT IN (
SELECT employee_id
FROM Employees
)
ORDER BY employee_id;
FROM Salaries
WHERE employee_id NOT IN (SELECT employee_id FROM Employees)
ORDER BY 1;
```

<!-- tabs:end -->
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
# Write your MySQL query statement below
SELECT employee_id
FROM Employees AS e
WHERE
e.employee_id NOT IN (
SELECT employee_id
FROM Salaries
)
FROM Employees
WHERE employee_id NOT IN (SELECT employee_id FROM Salaries)
UNION
SELECT employee_id
FROM Salaries AS s
WHERE
s.employee_id NOT IN (
SELECT employee_id
FROM Employees
)
ORDER BY employee_id;
FROM Salaries
WHERE employee_id NOT IN (SELECT employee_id FROM Employees)
ORDER BY 1;

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