Skip to content

Commit e0c580a

Browse files
committed
feat: add solutions to lcof2 problem: No.074
1 parent cc05b5b commit e0c580a

File tree

13 files changed

+448
-22
lines changed

13 files changed

+448
-22
lines changed

lcof2/剑指 Offer II 074. 合并区间/README.md

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,177 @@
4242

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

45+
区间合并,将所有存在交集的区间进行合并。
46+
47+
模板:
48+
49+
```py
50+
def merge(intervals):
51+
res = []
52+
intervals.sort(key=lambda x: x[0])
53+
st = ed = -1
54+
for s, e in intervals:
55+
if ed < s:
56+
if st != -1:
57+
res.append([st, ed])
58+
st, ed = e[0], e[1]
59+
else:
60+
ed = max(ed, e[1])
61+
if st != -1:
62+
res.append([st, ed])
63+
return res
64+
```
65+
4566
<!-- tabs:start -->
4667

4768
### **Python3**
4869

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

5172
```python
52-
73+
class Solution:
74+
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
75+
intervals.sort(key=lambda x: x[0])
76+
st = ed = -1
77+
res = []
78+
for s, e in intervals:
79+
if ed < s:
80+
if st != -1:
81+
res.append([st, ed])
82+
st, ed = s, e
83+
else:
84+
ed = max(ed, e)
85+
if st != -1:
86+
res.append([st, ed])
87+
return res
5388
```
5489

5590
### **Java**
5691

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

5994
```java
95+
class Solution {
96+
public int[][] merge(int[][] intervals) {
97+
Arrays.sort(intervals, Comparator.comparingInt(a -> a[0]));
98+
int st = -1, ed = -1;
99+
List<int[]> res = new ArrayList<>();
100+
for (int[] e : intervals) {
101+
if (ed < e[0]) {
102+
if (st != -1) {
103+
res.add(new int[]{st, ed});
104+
}
105+
st = e[0];
106+
ed = e[1];
107+
} else {
108+
ed = Math.max(ed, e[1]);
109+
}
110+
}
111+
if (st != -1) {
112+
res.add(new int[]{st, ed});
113+
}
114+
return res.toArray(new int[res.size()][]);
115+
}
116+
}
117+
```
118+
119+
### **C++**
120+
121+
```cpp
122+
class Solution {
123+
public:
124+
vector<vector<int>> merge(vector<vector<int>> &intervals) {
125+
sort(intervals.begin(), intervals.end());
126+
vector<vector<int>> res;
127+
int st = -1, ed = -1;
128+
for (auto e : intervals)
129+
{
130+
if (ed < e[0])
131+
{
132+
if (st != -1)
133+
{
134+
res.push_back({st, ed});
135+
}
136+
st = e[0];
137+
ed = e[1];
138+
}
139+
else
140+
{
141+
ed = max(ed, e[1]);
142+
}
143+
}
144+
if (st != -1)
145+
{
146+
res.push_back({st, ed});
147+
}
148+
return res;
149+
}
150+
};
151+
```
152+
153+
### **Go**
154+
155+
```go
156+
func merge(intervals [][]int) [][]int {
157+
var res [][]int
158+
sort.Slice(intervals, func(i, j int) bool {
159+
return intervals[i][0] < intervals[j][0]
160+
})
161+
st, ed := -1, -1
162+
for _, e := range intervals {
163+
if ed < e[0] {
164+
if st != -1 {
165+
res = append(res, []int{st, ed})
166+
}
167+
st, ed = e[0], e[1]
168+
} else {
169+
ed = max(ed, e[1])
170+
}
171+
}
172+
if st != -1 {
173+
res = append(res, []int{st, ed})
174+
}
175+
return res
176+
}
177+
178+
func max(a, b int) int {
179+
if a > b {
180+
return a
181+
}
182+
return b
183+
}
184+
```
60185

186+
### **C#**
187+
188+
```cpp
189+
public class Solution {
190+
public int[][] Merge(int[][] intervals) {
191+
var res = new List<int[]>();
192+
int st = -1, ed = -1;
193+
foreach (var e in intervals.OrderBy(a => a[0]))
194+
{
195+
if (ed < e[0])
196+
{
197+
if (st != -1)
198+
{
199+
res.Add(new int[] { st, ed });
200+
}
201+
st = e[0];
202+
ed = e[1];
203+
}
204+
else
205+
{
206+
ed = Math.Max(ed, e[1]);
207+
}
208+
}
209+
if (st != -1)
210+
{
211+
res.Add(new int[] { st, ed });
212+
}
213+
return res.ToArray();
214+
}
215+
}
61216
```
62217
63218
### **...**
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> merge(vector<vector<int>> &intervals) {
4+
sort(intervals.begin(), intervals.end());
5+
vector<vector<int>> res;
6+
int st = -1, ed = -1;
7+
for (auto e : intervals)
8+
{
9+
if (ed < e[0])
10+
{
11+
if (st != -1)
12+
{
13+
res.push_back({st, ed});
14+
}
15+
st = e[0];
16+
ed = e[1];
17+
}
18+
else
19+
{
20+
ed = max(ed, e[1]);
21+
}
22+
}
23+
if (st != -1)
24+
{
25+
res.push_back({st, ed});
26+
}
27+
return res;
28+
}
29+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class Solution {
2+
public int[][] Merge(int[][] intervals) {
3+
var res = new List<int[]>();
4+
int st = -1, ed = -1;
5+
foreach (var e in intervals.OrderBy(a => a[0]))
6+
{
7+
if (ed < e[0])
8+
{
9+
if (st != -1)
10+
{
11+
res.Add(new int[] { st, ed });
12+
}
13+
st = e[0];
14+
ed = e[1];
15+
}
16+
else
17+
{
18+
ed = Math.Max(ed, e[1]);
19+
}
20+
}
21+
if (st != -1)
22+
{
23+
res.Add(new int[] { st, ed });
24+
}
25+
return res.ToArray();
26+
}
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
func merge(intervals [][]int) [][]int {
2+
var res [][]int
3+
sort.Slice(intervals, func(i, j int) bool {
4+
return intervals[i][0] < intervals[j][0]
5+
})
6+
st, ed := -1, -1
7+
for _, e := range intervals {
8+
if ed < e[0] {
9+
if st != -1 {
10+
res = append(res, []int{st, ed})
11+
}
12+
st, ed = e[0], e[1]
13+
} else {
14+
ed = max(ed, e[1])
15+
}
16+
}
17+
if st != -1 {
18+
res = append(res, []int{st, ed})
19+
}
20+
return res
21+
}
22+
23+
func max(a, b int) int {
24+
if a > b {
25+
return a
26+
}
27+
return b
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int[][] merge(int[][] intervals) {
3+
Arrays.sort(intervals, Comparator.comparingInt(a -> a[0]));
4+
int st = -1, ed = -1;
5+
List<int[]> res = new ArrayList<>();
6+
for (int[] e : intervals) {
7+
if (ed < e[0]) {
8+
if (st != -1) {
9+
res.add(new int[]{st, ed});
10+
}
11+
st = e[0];
12+
ed = e[1];
13+
} else {
14+
ed = Math.max(ed, e[1]);
15+
}
16+
}
17+
if (st != -1) {
18+
res.add(new int[]{st, ed});
19+
}
20+
return res.toArray(new int[res.size()][]);
21+
}
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
3+
intervals.sort(key=lambda x: x[0])
4+
st = ed = -1
5+
res = []
6+
for s, e in intervals:
7+
if ed < s:
8+
if st != -1:
9+
res.append([st, ed])
10+
st, ed = s, e
11+
else:
12+
ed = max(ed, e)
13+
if st != -1:
14+
res.append([st, ed])
15+
return res
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# [1978. Employees Whose Manager Left the Company](https://leetcode-cn.com/problems/employees-whose-manager-left-the-company)
2+
3+
[English Version](/solution/1900-1999/1978.Employees%20Whose%20Manager%20Left%20the%20Company/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code>Employees</code></p>
10+
11+
<pre>
12+
+-------------+----------+
13+
| Column Name | Type |
14+
+-------------+----------+
15+
| employee_id | int |
16+
| name | varchar |
17+
| manager_id | int |
18+
| salary | int |
19+
+-------------+----------+
20+
employee_id is the primary key for this table.
21+
This table contains information about the employees, their salary, and the ID of their manager. Some employees do not have a manager (manager_id is null).
22+
</pre>
23+
24+
<p>&nbsp;</p>
25+
26+
<p>Write an SQL query to report the IDs of the employees whose salary is strictly less than <code>$30000</code> and whose manager left the company. When a manager leaves the company, their information is deleted from the <code>Employees</code> table, but the reports still have their <code>manager_id</code> set to the manager that left.</p>
27+
28+
<p>Return the result table ordered by <code>employee_id</code>.</p>
29+
30+
<p>The query result format is in the following example.</p>
31+
32+
<p>&nbsp;</p>
33+
<p><strong>Example 1:</strong></p>
34+
35+
<pre>
36+
<strong>Input: </strong>
37+
Employees table:
38+
+-------------+-----------+------------+--------+
39+
| employee_id | name | manager_id | salary |
40+
+-------------+-----------+------------+--------+
41+
| 3 | Mila | 9 | 60301 |
42+
| 12 | Antonella | null | 31000 |
43+
| 13 | Emery | null | 67084 |
44+
| 1 | Kalel | 11 | 21241 |
45+
| 9 | Mikaela | null | 50937 |
46+
| 11 | Joziah | 6 | 28485 |
47+
+-------------+-----------+------------+--------+
48+
<strong>Output:</strong>
49+
+-------------+
50+
| employee_id |
51+
+-------------+
52+
| 11 |
53+
+-------------+
54+
55+
<strong>Explanation:</strong>
56+
The employees with a salary less than $30000 are 1 (Kalel) and 11 (Joziah).
57+
Kalel&#39;s manager is employee 11, who is still in the company (Joziah).
58+
Joziah&#39;s manager is employee 6, who left the company because there is no row for employee 6 as it was deleted.
59+
</pre>
60+
61+
62+
## 解法
63+
64+
<!-- 这里可写通用的实现逻辑 -->
65+
66+
<!-- tabs:start -->
67+
68+
### **SQL**
69+
70+
<!-- 这里可写当前语言的特殊实现逻辑 -->
71+
72+
```sql
73+
74+
```
75+
76+
<!-- tabs:end -->

0 commit comments

Comments
 (0)