Skip to content

Commit a14e939

Browse files
authored
feat: add solutions to lc problem: No.3263 (doocs#3440)
No.3263.Convert Doubly Linked List to Array I
1 parent b098694 commit a14e939

File tree

16 files changed

+561
-91
lines changed

16 files changed

+561
-91
lines changed

solution/3200-3299/3262.Find Overlapping Shifts/README.md

+47-51
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ tags:
88

99
<!-- problem:start -->
1010

11-
# [3262. Find Overlapping Shifts 🔒](https://leetcode.cn/problems/find-overlapping-shifts)
11+
# [3262. 查找重叠的班次 🔒](https://leetcode.cn/problems/find-overlapping-shifts)
1212

1313
[English Version](/solution/3200-3299/3262.Find%20Overlapping%20Shifts/README_EN.md)
1414

1515
## 题目描述
1616

1717
<!-- description:start -->
1818

19-
<p>Table: <code>EmployeeShifts</code></p>
19+
<p>表:<code>EmployeeShifts</code></p>
2020

2121
<pre>
2222
+------------------+---------+
@@ -26,23 +26,24 @@ tags:
2626
| start_time | time |
2727
| end_time | time |
2828
+------------------+---------+
29-
(employee_id, start_time) is the unique key for this table.
30-
This table contains information about the shifts worked by employees, including the start and end times on a specific date.
29+
(employee_id, start_time) 是此表的唯一主键。
30+
这张表包含员工的排班工作,包括特定日期的开始和结束时间。
3131
</pre>
3232

33-
<p>Write a solution to count the number of <strong>overlapping shifts</strong> for each employee. Two shifts are considered overlapping if one shift&rsquo;s <code>end_time</code> is <strong>later than</strong> another shift&rsquo;s <code>start_time</code>.</p>
33+
<p>编写一个解决方案来为每个员工计算&nbsp;<strong>重叠排班</strong>&nbsp;的数量。如果一个排班的&nbsp;<code>end_time</code>&nbsp;比另一个排班的&nbsp;<code>start_time</code>&nbsp;<strong>更晚&nbsp;</strong>则认为两个排班重叠。</p>
3434

35-
<p><em>Return the result table ordered by</em> <code>employee_id</code> <em>in <strong>ascending</strong> order</em>.</p>
35+
<p>返回结果表以&nbsp;<code>employee_id</code> <strong>升序&nbsp;</strong>排序。</p>
3636

37-
<p>The query result format is in the following example.</p>
37+
<p>查询结果格式如下所示。</p>
3838

3939
<p>&nbsp;</p>
40-
<p><strong class="example">Example:</strong></p>
40+
41+
<p><b>示例:</b></p>
4142

4243
<div class="example-block">
43-
<p><strong>Input:</strong></p>
44+
<p><strong>输入:</strong></p>
4445

45-
<p><code>EmployeeShifts</code> table:</p>
46+
<p><code>EmployeeShifts</code> 表:</p>
4647

4748
<pre class="example-io">
4849
+-------------+------------+----------+
@@ -61,7 +62,7 @@ This table contains information about the shifts worked by employees, including
6162
+-------------+------------+----------+
6263
</pre>
6364

64-
<p><strong>Output:</strong></p>
65+
<p><strong>输出:</strong></p>
6566

6667
<pre class="example-io">
6768
+-------------+--------------------+
@@ -73,38 +74,38 @@ This table contains information about the shifts worked by employees, including
7374
+-------------+--------------------+
7475
</pre>
7576

76-
<p><strong>Explanation:</strong></p>
77+
<p><strong>解释:</strong></p>
7778

7879
<ul>
79-
<li>Employee 1 has 3 shifts:
80+
<li>员工 1 3 个排班:
8081
<ul>
81-
<li>08:00:00 to 12:00:00</li>
82-
<li>11:00:00 to 15:00:00</li>
83-
<li>14:00:00 to 18:00:00</li>
82+
<li>08:00:00 12:00:00</li>
83+
<li>11:00:00 15:00:00</li>
84+
<li>14:00:00 18:00:00</li>
8485
</ul>
85-
The first shift overlaps with the second, and the second overlaps with the third, resulting in 2 overlapping shifts.</li>
86-
<li>Employee 2 has 2 shifts:
86+
第一个排班与第二个排班重叠,第二个排班与第三个排班重叠,因此有 2&nbsp;个重叠排班。</li>
87+
<li>员工 2&nbsp;有 2 个排班:
8788
<ul>
88-
<li>09:00:00 to 17:00:00</li>
89-
<li>16:00:00 to 20:00:00</li>
89+
<li>09:00:00 17:00:00</li>
90+
<li>16:00:00 20:00:00</li>
9091
</ul>
91-
These shifts overlap with each other, resulting in 1 overlapping shift.</li>
92-
<li>Employee 3 has 3 shifts:
92+
这些排班彼此重叠,因此有 1 个重叠排班。</li>
93+
<li>员工 3 3 个排班:
9394
<ul>
94-
<li>10:00:00 to 12:00:00</li>
95-
<li>13:00:00 to 15:00:00</li>
96-
<li>16:00:00 to 18:00:00</li>
95+
<li>10:00:00 12:00:00</li>
96+
<li>13:00:00 15:00:00</li>
97+
<li>16:00:00 18:00:00</li>
9798
</ul>
98-
None of these shifts overlap, so Employee 3 is not included in the output.</li>
99-
<li>Employee 4 has 2 shifts:
99+
这些排班没有重叠,所以员工 3 不包含在输出中。</li>
100+
<li>员工 4 2 个排班:
100101
<ul>
101-
<li>08:00:00 to 10:00:00</li>
102-
<li>09:00:00 to 11:00:00</li>
102+
<li>08:00:00 10:00:00</li>
103+
<li>09:00:00 11:00:00</li>
103104
</ul>
104-
These shifts overlap with each other, resulting in 1 overlapping shift.</li>
105+
这些排班彼此重叠,因此有 1 个重叠排班。</li>
105106
</ul>
106107

107-
<p>The output shows the employee_id and the count of overlapping shifts for each employee who has at least one overlapping shift, ordered by employee_id in ascending order.</p>
108+
<p>输出展示了 employee_id 和至少有一个重叠排班的员工的重叠排班的数量,以 employee_id 升序排序。</p>
108109
</div>
109110

110111
<!-- description:end -->
@@ -117,13 +118,12 @@ This table contains information about the shifts worked by employees, including
117118

118119
我们首先使用自连接,将 `EmployeeShifts` 表连接自身。通过连接条件,确保只比较同一个员工的班次,并且检查班次之间是否存在重叠。
119120

120-
1. `t1.start_time < t2.end_time`:确保第一个班次的开始时间早于第二个班次的结束时间。
121+
1. `t1.start_time < t1.start_time`:确保第一个班次的开始时间早于第二个班次的结束时间。
121122
1. `t1.end_time > t2.start_time`:确保第一个班次的结束时间晚于第二个班次的开始时间。
122-
1. `t1.start_time != t2.start_time`:避免班次与自身比较。
123123

124-
接下来,我们对数据按照 `employee_id` 进行分组,统计每个员工的重叠班次数量。这里我们将重叠班次数量除以 2,因为我们在自连接时,每个重叠的班次都会被计算两次。
124+
接下来,我们对数据按照 `employee_id` 进行分组,统计每个员工的重叠班次数量。
125125

126-
最后,我们筛选出重叠班次数量大于 0 的员工,并按照 `employee_id` 进行升序排序。
126+
最后,我们筛选出重叠班次数量大于 $0$ 的员工,并按照 `employee_id` 进行升序排序。
127127

128128
<!-- tabs:start -->
129129

@@ -132,14 +132,13 @@ This table contains information about the shifts worked by employees, including
132132
```sql
133133
SELECT
134134
t1.employee_id,
135-
COUNT(*) / 2 AS overlapping_shifts
135+
COUNT(*) AS overlapping_shifts
136136
FROM
137137
EmployeeShifts t1
138138
JOIN EmployeeShifts t2
139139
ON t1.employee_id = t2.employee_id
140-
AND t1.start_time < t2.end_time
140+
AND t1.start_time < t2.start_time
141141
AND t1.end_time > t2.start_time
142-
AND t1.start_time != t2.start_time
143142
GROUP BY 1
144143
HAVING overlapping_shifts > 0
145144
ORDER BY 1;
@@ -152,23 +151,20 @@ import pandas as pd
152151

153152

154153
def find_overlapping_shifts(employee_shifts: pd.DataFrame) -> pd.DataFrame:
155-
merged = employee_shifts.merge(
156-
employee_shifts, on="employee_id", suffixes=("_1", "_2")
154+
merged_shifts = employee_shifts.merge(
155+
employee_shifts, on="employee_id", suffixes=("_t1", "_t2")
157156
)
158-
overlap = merged[
159-
(merged["start_time_1"] < merged["end_time_2"])
160-
& (merged["end_time_1"] > merged["start_time_2"])
161-
& (merged["start_time_1"] != merged["start_time_2"])
157+
overlapping_shifts = merged_shifts[
158+
(merged_shifts["start_time_t1"] < merged_shifts["start_time_t2"])
159+
& (merged_shifts["end_time_t1"] > merged_shifts["start_time_t2"])
162160
]
163-
overlap_counts = (
164-
overlap.groupby("employee_id").size().reset_index(name="overlapping_shifts")
165-
)
166-
overlap_counts["overlapping_shifts"] = overlap_counts["overlapping_shifts"] // 2
167161
result = (
168-
overlap_counts[overlap_counts["overlapping_shifts"] > 0]
169-
.sort_values("employee_id")
170-
.reset_index(drop=True)
162+
overlapping_shifts.groupby("employee_id")
163+
.size()
164+
.reset_index(name="overlapping_shifts")
171165
)
166+
result = result[result["overlapping_shifts"] > 0]
167+
result = result.sort_values("employee_id").reset_index(drop=True)
172168
return result
173169
```
174170

solution/3200-3299/3262.Find Overlapping Shifts/README_EN.md

+17-22
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,16 @@ This table contains information about the shifts worked by employees, including
113113

114114
<!-- solution:start -->
115115

116-
### Solution 1: Self-Join + Group Count
116+
### Solution 1: Self-Join + Group Counting
117117

118-
We start by using a self-join to join the `EmployeeShifts` table with itself. The join condition ensures that only shifts of the same employee are compared, and checks if there is any overlap between the shifts.
118+
We first use a self-join to connect the `EmployeeShifts` table to itself. The join condition ensures that we only compare shifts belonging to the same employee and check if there is any overlap between shifts.
119119

120-
1. `t1.start_time < t2.end_time`: Ensures that the start time of the first shift is earlier than the end time of the second shift.
120+
1. `t1.start_time < t2.start_time`: Ensures that the start time of the first shift is earlier than the start time of the second shift.
121121
2. `t1.end_time > t2.start_time`: Ensures that the end time of the first shift is later than the start time of the second shift.
122-
3. `t1.start_time != t2.start_time`: Avoids comparing a shift with itself.
123122

124-
Next, we group the data by `employee_id` and count the number of overlapping shifts for each employee. We divide the count by 2 because each overlap is counted twice in the self-join.
123+
Next, we group the data by `employee_id` and count the number of overlapping shifts for each employee.
125124

126-
Finally, we filter out employees with an overlapping shift count greater than 0 and sort the results in ascending order by `employee_id`.
125+
Finally, we filter out employees with overlapping shift counts greater than $0$ and sort the results in ascending order by `employee_id`.
127126

128127
<!-- tabs:start -->
129128

@@ -132,14 +131,13 @@ Finally, we filter out employees with an overlapping shift count greater than 0
132131
```sql
133132
SELECT
134133
t1.employee_id,
135-
COUNT(*) / 2 AS overlapping_shifts
134+
COUNT(*) AS overlapping_shifts
136135
FROM
137136
EmployeeShifts t1
138137
JOIN EmployeeShifts t2
139138
ON t1.employee_id = t2.employee_id
140-
AND t1.start_time < t2.end_time
139+
AND t1.start_time < t2.start_time
141140
AND t1.end_time > t2.start_time
142-
AND t1.start_time != t2.start_time
143141
GROUP BY 1
144142
HAVING overlapping_shifts > 0
145143
ORDER BY 1;
@@ -152,23 +150,20 @@ import pandas as pd
152150

153151

154152
def find_overlapping_shifts(employee_shifts: pd.DataFrame) -> pd.DataFrame:
155-
merged = employee_shifts.merge(
156-
employee_shifts, on="employee_id", suffixes=("_1", "_2")
153+
merged_shifts = employee_shifts.merge(
154+
employee_shifts, on="employee_id", suffixes=("_t1", "_t2")
157155
)
158-
overlap = merged[
159-
(merged["start_time_1"] < merged["end_time_2"])
160-
& (merged["end_time_1"] > merged["start_time_2"])
161-
& (merged["start_time_1"] != merged["start_time_2"])
156+
overlapping_shifts = merged_shifts[
157+
(merged_shifts["start_time_t1"] < merged_shifts["start_time_t2"])
158+
& (merged_shifts["end_time_t1"] > merged_shifts["start_time_t2"])
162159
]
163-
overlap_counts = (
164-
overlap.groupby("employee_id").size().reset_index(name="overlapping_shifts")
165-
)
166-
overlap_counts["overlapping_shifts"] = overlap_counts["overlapping_shifts"] // 2
167160
result = (
168-
overlap_counts[overlap_counts["overlapping_shifts"] > 0]
169-
.sort_values("employee_id")
170-
.reset_index(drop=True)
161+
overlapping_shifts.groupby("employee_id")
162+
.size()
163+
.reset_index(name="overlapping_shifts")
171164
)
165+
result = result[result["overlapping_shifts"] > 0]
166+
result = result.sort_values("employee_id").reset_index(drop=True)
172167
return result
173168
```
174169

solution/3200-3299/3262.Find Overlapping Shifts/Solution.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@
22

33

44
def find_overlapping_shifts(employee_shifts: pd.DataFrame) -> pd.DataFrame:
5-
merged = employee_shifts.merge(
6-
employee_shifts, on="employee_id", suffixes=("_1", "_2")
5+
merged_shifts = employee_shifts.merge(
6+
employee_shifts, on="employee_id", suffixes=("_t1", "_t2")
77
)
8-
overlap = merged[
9-
(merged["start_time_1"] < merged["end_time_2"])
10-
& (merged["end_time_1"] > merged["start_time_2"])
11-
& (merged["start_time_1"] != merged["start_time_2"])
8+
overlapping_shifts = merged_shifts[
9+
(merged_shifts["start_time_t1"] < merged_shifts["start_time_t2"])
10+
& (merged_shifts["end_time_t1"] > merged_shifts["start_time_t2"])
1211
]
13-
overlap_counts = (
14-
overlap.groupby("employee_id").size().reset_index(name="overlapping_shifts")
15-
)
16-
overlap_counts["overlapping_shifts"] = overlap_counts["overlapping_shifts"] // 2
1712
result = (
18-
overlap_counts[overlap_counts["overlapping_shifts"] > 0]
19-
.sort_values("employee_id")
20-
.reset_index(drop=True)
13+
overlapping_shifts.groupby("employee_id")
14+
.size()
15+
.reset_index(name="overlapping_shifts")
2116
)
17+
result = result[result["overlapping_shifts"] > 0]
18+
result = result.sort_values("employee_id").reset_index(drop=True)
2219
return result
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
SELECT
22
t1.employee_id,
3-
COUNT(*) / 2 AS overlapping_shifts
3+
COUNT(*) AS overlapping_shifts
44
FROM
55
EmployeeShifts t1
66
JOIN EmployeeShifts t2
77
ON t1.employee_id = t2.employee_id
8-
AND t1.start_time < t2.end_time
8+
AND t1.start_time < t2.start_time
99
AND t1.end_time > t2.start_time
10-
AND t1.start_time != t2.start_time
1110
GROUP BY 1
1211
HAVING overlapping_shifts > 0
1312
ORDER BY 1;

0 commit comments

Comments
 (0)