Skip to content

Commit 9203729

Browse files
authored
feat: add solutions to lc problems: No.3052,3057,3060 (#2385)
1 parent 44db05f commit 9203729

File tree

9 files changed

+200
-12
lines changed

9 files changed

+200
-12
lines changed

solution/3000-3099/3052.Maximize Items/README.md

+27-2
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,37 @@ Output table is ordered by item count in descending order.</pre>
7272

7373
## 解法
7474

75-
### 方法一
75+
### 方法一:连接查询 + 合并
76+
77+
我们先计算出所有 prime_eligible 类型的物品的总面积,记录在 `T` 表的 `s` 字段中。
78+
79+
接下来,我们分别计算 prime_eligible 和 not_prime 类型的物品的数量。对于 prime_eligible 类型的物品,我们可以存储的份数是 $\lfloor \frac{500000}{s} \rfloor$,对于 not_prime 类型的物品,我们可以存储的份数是 $\lfloor \frac{500000 \mod s}{\sum \text{s1}} \rfloor$。其中 $\sum \text{s1}$ 是所有 not_prime 类型的物品的总面积。再分别乘上 prime_eligible 和 not_prime 类型的物品的数量,就是我们的结果。
7680

7781
<!-- tabs:start -->
7882

7983
```sql
80-
84+
# Write your MySQL query statement below
85+
WITH
86+
T AS (
87+
SELECT SUM(square_footage) AS s
88+
FROM Inventory
89+
WHERE item_type = 'prime_eligible'
90+
)
91+
SELECT
92+
'prime_eligible' AS item_type,
93+
COUNT(1) * FLOOR(500000 / s) AS item_count
94+
FROM
95+
Inventory
96+
JOIN T
97+
WHERE item_type = 'prime_eligible'
98+
UNION ALL
99+
SELECT
100+
'not_prime',
101+
IFNULL(COUNT(1) * FLOOR(IF(s = 0, 500000, 500000 % s) / SUM(square_footage)), 0)
102+
FROM
103+
Inventory
104+
JOIN T
105+
WHERE item_type = 'not_prime';
81106
```
82107

83108
<!-- tabs:end -->

solution/3000-3099/3052.Maximize Items/README_EN.md

+27-2
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,37 @@ Output table is ordered by item count in descending order.</pre>
7070

7171
## Solutions
7272

73-
### Solution 1
73+
### Solution 1: Join Query + Union All
74+
75+
First, we calculate the total area of all items of type `prime_eligible` and record it in the `s` field of table `T`.
76+
77+
Next, we calculate the number of items of type `prime_eligible` and `not_prime` respectively. For items of type `prime_eligible`, the number of portions we can store is $\lfloor \frac{500000}{s} \rfloor$. For items of type `not_prime`, the number of portions we can store is $\lfloor \frac{500000 \mod s}{\sum \text{s1}} \rfloor$. Where $\sum \text{s1}$ is the total area of all items of type `not_prime`. Multiplying by the number of items of type `prime_eligible` and `not_prime` respectively gives us our result.
7478

7579
<!-- tabs:start -->
7680

7781
```sql
78-
82+
# Write your MySQL query statement below
83+
WITH
84+
T AS (
85+
SELECT SUM(square_footage) AS s
86+
FROM Inventory
87+
WHERE item_type = 'prime_eligible'
88+
)
89+
SELECT
90+
'prime_eligible' AS item_type,
91+
COUNT(1) * FLOOR(500000 / s) AS item_count
92+
FROM
93+
Inventory
94+
JOIN T
95+
WHERE item_type = 'prime_eligible'
96+
UNION ALL
97+
SELECT
98+
'not_prime',
99+
IFNULL(COUNT(1) * FLOOR(IF(s = 0, 500000, 500000 % s) / SUM(square_footage)), 0)
100+
FROM
101+
Inventory
102+
JOIN T
103+
WHERE item_type = 'not_prime';
79104
```
80105

81106
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT SUM(square_footage) AS s
5+
FROM Inventory
6+
WHERE item_type = 'prime_eligible'
7+
)
8+
SELECT
9+
'prime_eligible' AS item_type,
10+
COUNT(1) * FLOOR(500000 / s) AS item_count
11+
FROM
12+
Inventory
13+
JOIN T
14+
WHERE item_type = 'prime_eligible'
15+
UNION ALL
16+
SELECT
17+
'not_prime',
18+
IFNULL(COUNT(1) * FLOOR(IF(s = 0, 500000, 500000 % s) / SUM(square_footage)), 0)
19+
FROM
20+
Inventory
21+
JOIN T
22+
WHERE item_type = 'not_prime';

solution/3000-3099/3057.Employees Project Allocation/README.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,35 @@ Result table orderd by employee_id, project_id in ascending order.
8383

8484
## 解法
8585

86-
### 方法一
86+
### 方法一:分组统计 + 等值连接
87+
88+
我们先根据 `employee_id` 连接 `Project` 表和 `Employees` 表,然后再根据 `team` 分组统计每个团队的平均工作量,记录在临时表 `T` 中。
89+
90+
然后,我们再次连接 `Project` 表和 `Employees` 表,同时连接 `T` 表,找出工作量大于团队平均工作量的员工,并且按照 `employee_id``project_id` 排序。
8791

8892
<!-- tabs:start -->
8993

9094
```sql
91-
95+
# Write your MySQL query statement below
96+
WITH
97+
T AS (
98+
SELECT team, AVG(workload) AS avg_workload
99+
FROM
100+
Project
101+
JOIN Employees USING (employee_id)
102+
GROUP BY 1
103+
)
104+
SELECT
105+
employee_id,
106+
project_id,
107+
name AS employee_name,
108+
workload AS project_workload
109+
FROM
110+
Project
111+
JOIN Employees USING (employee_id)
112+
JOIN T USING (team)
113+
WHERE workload > avg_workload
114+
ORDER BY 1, 2;
92115
```
93116

94117
<!-- tabs:end -->

solution/3000-3099/3057.Employees Project Allocation/README_EN.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,35 @@ Result table orderd by employee_id, project_id in ascending order.
8181

8282
## Solutions
8383

84-
### Solution 1
84+
### Solution 1: Grouping Statistics + Equi-Join
85+
86+
First, we join the `Project` table and the `Employees` table based on `employee_id`, then group by `team` to calculate the average workload of each team, and record it in the temporary table `T`.
87+
88+
Then, we join the `Project` table and the `Employees` table again, and also join the `T` table, to find employees whose workload is greater than the average workload of the team. Finally, we sort by `employee_id` and `project_id`.
8589

8690
<!-- tabs:start -->
8791

8892
```sql
89-
93+
# Write your MySQL query statement below
94+
WITH
95+
T AS (
96+
SELECT team, AVG(workload) AS avg_workload
97+
FROM
98+
Project
99+
JOIN Employees USING (employee_id)
100+
GROUP BY 1
101+
)
102+
SELECT
103+
employee_id,
104+
project_id,
105+
name AS employee_name,
106+
workload AS project_workload
107+
FROM
108+
Project
109+
JOIN Employees USING (employee_id)
110+
JOIN T USING (team)
111+
WHERE workload > avg_workload
112+
ORDER BY 1, 2;
90113
```
91114

92115
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT team, AVG(workload) AS avg_workload
5+
FROM
6+
Project
7+
JOIN Employees USING (employee_id)
8+
GROUP BY 1
9+
)
10+
SELECT
11+
employee_id,
12+
project_id,
13+
name AS employee_name,
14+
workload AS project_workload
15+
FROM
16+
Project
17+
JOIN Employees USING (employee_id)
18+
JOIN T USING (team)
19+
WHERE workload > avg_workload
20+
ORDER BY 1, 2;

solution/3000-3099/3060.User Activities within Time Bounds/README.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,29 @@ Output table is ordered by user_id in increasing order.
6868

6969
## 解法
7070

71-
### 方法一
71+
### 方法一:窗口函数 + 时间函数
72+
73+
我们先使用 `LAG` 窗口函数,找到每个用户相同类型的会话的上一个会话的结束时间,记为 `prev_session_end`。然后我们使用 `TIMESTAMPDIFF` 函数计算当前会话的开始时间与上一个会话的结束时间的时间差,如果时间差小于等于 12 小时,那么这个用户就符合题目要求。
7274

7375
<!-- tabs:start -->
7476

7577
```sql
76-
78+
# Write your MySQL query statement below
79+
WITH
80+
T AS (
81+
SELECT
82+
user_id,
83+
session_start,
84+
LAG(session_end) OVER (
85+
PARTITION BY user_id, session_type
86+
ORDER BY session_end
87+
) AS prev_session_end
88+
FROM Sessions
89+
)
90+
SELECT DISTINCT
91+
user_id
92+
FROM T
93+
WHERE TIMESTAMPDIFF(HOUR, prev_session_end, session_start) <= 12;
7794
```
7895

7996
<!-- tabs:end -->

solution/3000-3099/3060.User Activities within Time Bounds/README_EN.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,29 @@ Output table is ordered by user_id in increasing order.
6666

6767
## Solutions
6868

69-
### Solution 1
69+
### Solution 1: Window Function + Time Function
70+
71+
First, we use the `LAG` window function to find the end time of the previous session of the same type for each user, denoted as `prev_session_end`. Then we use the `TIMESTAMPDIFF` function to calculate the time difference between the start time of the current session and the end time of the previous session. If the time difference is less than or equal to 12 hours, then this user meets the requirements of the problem.
7072

7173
<!-- tabs:start -->
7274

7375
```sql
74-
76+
# Write your MySQL query statement below
77+
WITH
78+
T AS (
79+
SELECT
80+
user_id,
81+
session_start,
82+
LAG(session_end) OVER (
83+
PARTITION BY user_id, session_type
84+
ORDER BY session_end
85+
) AS prev_session_end
86+
FROM Sessions
87+
)
88+
SELECT DISTINCT
89+
user_id
90+
FROM T
91+
WHERE TIMESTAMPDIFF(HOUR, prev_session_end, session_start) <= 12;
7592
```
7693

7794
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT
5+
user_id,
6+
session_start,
7+
LAG(session_end) OVER (
8+
PARTITION BY user_id, session_type
9+
ORDER BY session_end
10+
) AS prev_session_end
11+
FROM Sessions
12+
)
13+
SELECT DISTINCT
14+
user_id
15+
FROM T
16+
WHERE TIMESTAMPDIFF(HOUR, prev_session_end, session_start) <= 12;

0 commit comments

Comments
 (0)