Skip to content

Commit 350027c

Browse files
committed
feat: add solutions to lc problem: No.1229
No.1229.Meeting Scheduler
1 parent cddc493 commit 350027c

File tree

6 files changed

+297
-2
lines changed

6 files changed

+297
-2
lines changed

solution/1200-1299/1229.Meeting Scheduler/README.md

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,127 @@
4747

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

50+
**方法一:排序 + 双指针**
51+
52+
我们可以将两个人的空闲时间分别排序,然后使用双指针遍历两个数组,找到两个人的空闲时间段的交集,如果交集的长度大于等于 `duration`,则返回交集的起始时间和起始时间加上 `duration`
53+
54+
时间复杂度 $O(m \times \log m + n \times \log n)$,空间复杂度 $O(\log m + \log n)$。其中 $m$ 和 $n$ 分别为两个数组的长度。
55+
5056
<!-- tabs:start -->
5157

5258
### **Python3**
5359

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

5662
```python
57-
63+
class Solution:
64+
def minAvailableDuration(self, slots1: List[List[int]], slots2: List[List[int]], duration: int) -> List[int]:
65+
slots1.sort()
66+
slots2.sort()
67+
m, n = len(slots1), len(slots2)
68+
i = j = 0
69+
while i < m and j < n:
70+
start = max(slots1[i][0], slots2[j][0])
71+
end = min(slots1[i][1], slots2[j][1])
72+
if end - start >= duration:
73+
return [start, start + duration]
74+
if slots1[i][1] < slots2[j][1]:
75+
i += 1
76+
else:
77+
j += 1
78+
return []
5879
```
5980

6081
### **Java**
6182

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

6485
```java
86+
class Solution {
87+
public List<Integer> minAvailableDuration(int[][] slots1, int[][] slots2, int duration) {
88+
Arrays.sort(slots1, (a, b) -> a[0] - b[0]);
89+
Arrays.sort(slots2, (a, b) -> a[0] - b[0]);
90+
int m = slots1.length, n = slots2.length;
91+
int i = 0, j = 0;
92+
while (i < m && j < n) {
93+
int start = Math.max(slots1[i][0], slots2[j][0]);
94+
int end = Math.min(slots1[i][1], slots2[j][1]);
95+
if (end - start >= duration) {
96+
return Arrays.asList(start, start + duration);
97+
}
98+
if (slots1[i][1] < slots2[j][1]) {
99+
++i;
100+
} else {
101+
++j;
102+
}
103+
}
104+
return Collections.emptyList();
105+
}
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
vector<int> minAvailableDuration(vector<vector<int>>& slots1, vector<vector<int>>& slots2, int duration) {
115+
sort(slots1.begin(), slots1.end());
116+
sort(slots2.begin(), slots2.end());
117+
int m = slots1.size(), n = slots2.size();
118+
int i = 0, j = 0;
119+
while (i < m && j < n) {
120+
int start = max(slots1[i][0], slots2[j][0]);
121+
int end = min(slots1[i][1], slots2[j][1]);
122+
if (end - start >= duration) {
123+
return {start, start + duration};
124+
}
125+
if (slots1[i][1] < slots2[j][1]) {
126+
++i;
127+
} else {
128+
++j;
129+
}
130+
}
131+
return {};
132+
}
133+
};
134+
```
65135
136+
### **Go**
137+
138+
```go
139+
func minAvailableDuration(slots1 [][]int, slots2 [][]int, duration int) []int {
140+
sort.Slice(slots1, func(i, j int) bool { return slots1[i][0] < slots1[j][0] })
141+
sort.Slice(slots2, func(i, j int) bool { return slots2[i][0] < slots2[j][0] })
142+
i, j, m, n := 0, 0, len(slots1), len(slots2)
143+
for i < m && j < n {
144+
start := max(slots1[i][0], slots2[j][0])
145+
end := min(slots1[i][1], slots2[j][1])
146+
if end-start >= duration {
147+
return []int{start, start + duration}
148+
}
149+
if slots1[i][1] < slots2[j][1] {
150+
i++
151+
} else {
152+
j++
153+
}
154+
}
155+
return []int{}
156+
}
157+
158+
func max(a, b int) int {
159+
if a > b {
160+
return a
161+
}
162+
return b
163+
}
164+
165+
func min(a, b int) int {
166+
if a < b {
167+
return a
168+
}
169+
return b
170+
}
66171
```
67172

68173
### **...**

solution/1200-1299/1229.Meeting Scheduler/README_EN.md

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,112 @@
4646
### **Python3**
4747

4848
```python
49-
49+
class Solution:
50+
def minAvailableDuration(self, slots1: List[List[int]], slots2: List[List[int]], duration: int) -> List[int]:
51+
slots1.sort()
52+
slots2.sort()
53+
m, n = len(slots1), len(slots2)
54+
i = j = 0
55+
while i < m and j < n:
56+
start = max(slots1[i][0], slots2[j][0])
57+
end = min(slots1[i][1], slots2[j][1])
58+
if end - start >= duration:
59+
return [start, start + duration]
60+
if slots1[i][1] < slots2[j][1]:
61+
i += 1
62+
else:
63+
j += 1
64+
return []
5065
```
5166

5267
### **Java**
5368

5469
```java
70+
class Solution {
71+
public List<Integer> minAvailableDuration(int[][] slots1, int[][] slots2, int duration) {
72+
Arrays.sort(slots1, (a, b) -> a[0] - b[0]);
73+
Arrays.sort(slots2, (a, b) -> a[0] - b[0]);
74+
int m = slots1.length, n = slots2.length;
75+
int i = 0, j = 0;
76+
while (i < m && j < n) {
77+
int start = Math.max(slots1[i][0], slots2[j][0]);
78+
int end = Math.min(slots1[i][1], slots2[j][1]);
79+
if (end - start >= duration) {
80+
return Arrays.asList(start, start + duration);
81+
}
82+
if (slots1[i][1] < slots2[j][1]) {
83+
++i;
84+
} else {
85+
++j;
86+
}
87+
}
88+
return Collections.emptyList();
89+
}
90+
}
91+
```
92+
93+
### **C++**
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
vector<int> minAvailableDuration(vector<vector<int>>& slots1, vector<vector<int>>& slots2, int duration) {
99+
sort(slots1.begin(), slots1.end());
100+
sort(slots2.begin(), slots2.end());
101+
int m = slots1.size(), n = slots2.size();
102+
int i = 0, j = 0;
103+
while (i < m && j < n) {
104+
int start = max(slots1[i][0], slots2[j][0]);
105+
int end = min(slots1[i][1], slots2[j][1]);
106+
if (end - start >= duration) {
107+
return {start, start + duration};
108+
}
109+
if (slots1[i][1] < slots2[j][1]) {
110+
++i;
111+
} else {
112+
++j;
113+
}
114+
}
115+
return {};
116+
}
117+
};
118+
```
55119
120+
### **Go**
121+
122+
```go
123+
func minAvailableDuration(slots1 [][]int, slots2 [][]int, duration int) []int {
124+
sort.Slice(slots1, func(i, j int) bool { return slots1[i][0] < slots1[j][0] })
125+
sort.Slice(slots2, func(i, j int) bool { return slots2[i][0] < slots2[j][0] })
126+
i, j, m, n := 0, 0, len(slots1), len(slots2)
127+
for i < m && j < n {
128+
start := max(slots1[i][0], slots2[j][0])
129+
end := min(slots1[i][1], slots2[j][1])
130+
if end-start >= duration {
131+
return []int{start, start + duration}
132+
}
133+
if slots1[i][1] < slots2[j][1] {
134+
i++
135+
} else {
136+
j++
137+
}
138+
}
139+
return []int{}
140+
}
141+
142+
func max(a, b int) int {
143+
if a > b {
144+
return a
145+
}
146+
return b
147+
}
148+
149+
func min(a, b int) int {
150+
if a < b {
151+
return a
152+
}
153+
return b
154+
}
56155
```
57156

58157
### **...**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
vector<int> minAvailableDuration(vector<vector<int>>& slots1, vector<vector<int>>& slots2, int duration) {
4+
sort(slots1.begin(), slots1.end());
5+
sort(slots2.begin(), slots2.end());
6+
int m = slots1.size(), n = slots2.size();
7+
int i = 0, j = 0;
8+
while (i < m && j < n) {
9+
int start = max(slots1[i][0], slots2[j][0]);
10+
int end = min(slots1[i][1], slots2[j][1]);
11+
if (end - start >= duration) {
12+
return {start, start + duration};
13+
}
14+
if (slots1[i][1] < slots2[j][1]) {
15+
++i;
16+
} else {
17+
++j;
18+
}
19+
}
20+
return {};
21+
}
22+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
func minAvailableDuration(slots1 [][]int, slots2 [][]int, duration int) []int {
2+
sort.Slice(slots1, func(i, j int) bool { return slots1[i][0] < slots1[j][0] })
3+
sort.Slice(slots2, func(i, j int) bool { return slots2[i][0] < slots2[j][0] })
4+
i, j, m, n := 0, 0, len(slots1), len(slots2)
5+
for i < m && j < n {
6+
start := max(slots1[i][0], slots2[j][0])
7+
end := min(slots1[i][1], slots2[j][1])
8+
if end-start >= duration {
9+
return []int{start, start + duration}
10+
}
11+
if slots1[i][1] < slots2[j][1] {
12+
i++
13+
} else {
14+
j++
15+
}
16+
}
17+
return []int{}
18+
}
19+
20+
func max(a, b int) int {
21+
if a > b {
22+
return a
23+
}
24+
return b
25+
}
26+
27+
func min(a, b int) int {
28+
if a < b {
29+
return a
30+
}
31+
return b
32+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public List<Integer> minAvailableDuration(int[][] slots1, int[][] slots2, int duration) {
3+
Arrays.sort(slots1, (a, b) -> a[0] - b[0]);
4+
Arrays.sort(slots2, (a, b) -> a[0] - b[0]);
5+
int m = slots1.length, n = slots2.length;
6+
int i = 0, j = 0;
7+
while (i < m && j < n) {
8+
int start = Math.max(slots1[i][0], slots2[j][0]);
9+
int end = Math.min(slots1[i][1], slots2[j][1]);
10+
if (end - start >= duration) {
11+
return Arrays.asList(start, start + duration);
12+
}
13+
if (slots1[i][1] < slots2[j][1]) {
14+
++i;
15+
} else {
16+
++j;
17+
}
18+
}
19+
return Collections.emptyList();
20+
}
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def minAvailableDuration(self, slots1: List[List[int]], slots2: List[List[int]], duration: int) -> List[int]:
3+
slots1.sort()
4+
slots2.sort()
5+
m, n = len(slots1), len(slots2)
6+
i = j = 0
7+
while i < m and j < n:
8+
start = max(slots1[i][0], slots2[j][0])
9+
end = min(slots1[i][1], slots2[j][1])
10+
if end - start >= duration:
11+
return [start, start + duration]
12+
if slots1[i][1] < slots2[j][1]:
13+
i += 1
14+
else:
15+
j += 1
16+
return []

0 commit comments

Comments
 (0)