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

Commit daa41ea

Browse files
committed
feat: add solutions to lc problem: No.0759
1 parent d2b7dbd commit daa41ea

File tree

7 files changed

+618
-8
lines changed

7 files changed

+618
-8
lines changed

‎solution/0700-0799/0759.Employee Free Time/README.md‎

Lines changed: 209 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,237 @@ tags:
6868

6969
<!-- solution:start -->
7070

71-
### 方法一
71+
### 方法一:区间合并
72+
73+
我们可以将所有员工的工作时间区间合并成一个列表,然后对该列表进行排序并合并重叠的区间。最后,遍历合并后的区间列表,找出相邻区间之间的空闲时间段。
74+
75+
时间复杂度 $O(m \times n \times \log(m \times n)),ドル空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别为员工数量和每个员工的工作时间区间数量。
7276

7377
<!-- tabs:start -->
7478

7579
#### Python3
7680

7781
```python
78-
82+
"""
83+
# Definition for an Interval.
84+
class Interval:
85+
def __init__(self, start: int = None, end: int = None):
86+
self.start = start
87+
self.end = end
88+
"""
89+
90+
91+
class Solution:
92+
def employeeFreeTime(self, schedule: "[[Interval]]") -> "[Interval]":
93+
intervals = []
94+
for e in schedule:
95+
intervals.extend(e)
96+
intervals.sort(key=lambda x: (x.start, x.end))
97+
merged = [intervals[0]]
98+
for x in intervals[1:]:
99+
if merged[-1].end < x.start:
100+
merged.append(x)
101+
else:
102+
merged[-1].end = max(merged[-1].end, x.end)
103+
ans = []
104+
for a, b in pairwise(merged):
105+
ans.append(Interval(a.end, b.start))
106+
return ans
79107
```
80108

81109
#### Java
82110

83111
```java
84-
112+
/*
113+
// Definition for an Interval.
114+
class Interval {
115+
public int start;
116+
public int end;
117+
118+
public Interval() {}
119+
120+
public Interval(int _start, int _end) {
121+
start = _start;
122+
end = _end;
123+
}
124+
};
125+
*/
126+
127+
class Solution {
128+
public List<Interval> employeeFreeTime(List<List<Interval>> schedule) {
129+
List<Interval> intervals = new ArrayList<>();
130+
for (List<Interval> e : schedule) {
131+
intervals.addAll(e);
132+
}
133+
134+
intervals.sort((a, b) -> a.start == b.start ? a.end - b.end : a.start - b.start);
135+
136+
List<Interval> merged = new ArrayList<>();
137+
merged.add(intervals.get(0));
138+
for (int i = 1; i < intervals.size(); ++i) {
139+
Interval last = merged.get(merged.size() - 1);
140+
Interval cur = intervals.get(i);
141+
if (last.end < cur.start) {
142+
merged.add(cur);
143+
} else {
144+
last.end = Math.max(last.end, cur.end);
145+
}
146+
}
147+
148+
List<Interval> ans = new ArrayList<>();
149+
for (int i = 1; i < merged.size(); ++i) {
150+
Interval a = merged.get(i - 1);
151+
Interval b = merged.get(i);
152+
ans.add(new Interval(a.end, b.start));
153+
}
154+
155+
return ans;
156+
}
157+
}
85158
```
86159

87160
#### C++
88161

89162
```cpp
90-
163+
/*
164+
// Definition for an Interval.
165+
class Interval {
166+
public:
167+
int start;
168+
int end;
169+
170+
Interval() {}
171+
172+
Interval(int _start, int _end) {
173+
start = _start;
174+
end = _end;
175+
}
176+
};
177+
*/
178+
179+
class Solution {
180+
public:
181+
vector<Interval> employeeFreeTime(vector<vector<Interval>> schedule) {
182+
vector<Interval> intervals;
183+
for (auto& e : schedule) {
184+
intervals.insert(intervals.end(), e.begin(), e.end());
185+
}
186+
187+
sort(intervals.begin(), intervals.end(), [](const Interval& a, const Interval& b) {
188+
if (a.start == b.start) return a.end < b.end;
189+
return a.start < b.start;
190+
});
191+
192+
vector<Interval> merged;
193+
merged.push_back(intervals[0]);
194+
for (int i = 1; i < intervals.size(); ++i) {
195+
auto& last = merged.back();
196+
auto& cur = intervals[i];
197+
if (last.end < cur.start) {
198+
merged.push_back(cur);
199+
} else {
200+
last.end = max(last.end, cur.end);
201+
}
202+
}
203+
204+
vector<Interval> ans;
205+
for (int i = 1; i < merged.size(); ++i) {
206+
auto& a = merged[i - 1];
207+
auto& b = merged[i];
208+
ans.emplace_back(a.end, b.start);
209+
}
210+
211+
return ans;
212+
}
213+
};
91214
```
92215

93216
#### Go
94217

95218
```go
219+
/**
220+
* Definition for an Interval.
221+
* type Interval struct {
222+
* Start int
223+
* End int
224+
* }
225+
*/
226+
227+
func employeeFreeTime(schedule [][]*Interval) []*Interval {
228+
var intervals []*Interval
229+
for _, e := range schedule {
230+
intervals = append(intervals, e...)
231+
}
232+
233+
sort.Slice(intervals, func(i, j int) bool {
234+
if intervals[i].Start == intervals[j].Start {
235+
return intervals[i].End < intervals[j].End
236+
}
237+
return intervals[i].Start < intervals[j].Start
238+
})
239+
240+
merged := []*Interval{intervals[0]}
241+
for _, cur := range intervals[1:] {
242+
last := merged[len(merged)-1]
243+
if last.End < cur.Start {
244+
merged = append(merged, cur)
245+
} else if cur.End > last.End {
246+
last.End = cur.End
247+
}
248+
}
249+
250+
var ans []*Interval
251+
for i := 1; i < len(merged); i++ {
252+
a, b := merged[i-1], merged[i]
253+
ans = append(ans, &Interval{Start: a.End, End: b.Start})
254+
}
255+
256+
return ans
257+
}
258+
```
96259

260+
#### TypeScript
261+
262+
```ts
263+
/**
264+
* // Definition for an Interval.
265+
* class Interval {
266+
* start: number;
267+
* end: number;
268+
* constructor(start: number, end: number) {
269+
* this.start = start;
270+
* this.end = end;
271+
* }
272+
* }
273+
*/
274+
function employeeFreeTime(schedule: Interval[][]): Interval[] {
275+
const intervals: Interval[] = [];
276+
for (const e of schedule) {
277+
intervals.push(...e);
278+
}
279+
280+
intervals.sort((a, b) => (a.start === b.start ? a.end - b.end : a.start - b.start));
281+
282+
const merged: Interval[] = [intervals[0]];
283+
for (let i = 1; i < intervals.length; ++i) {
284+
const last = merged[merged.length - 1];
285+
const cur = intervals[i];
286+
if (last.end < cur.start) {
287+
merged.push(cur);
288+
} else {
289+
last.end = Math.max(last.end, cur.end);
290+
}
291+
}
292+
293+
const ans: Interval[] = [];
294+
for (let i = 1; i < merged.length; ++i) {
295+
const a = merged[i - 1];
296+
const b = merged[i];
297+
ans.push(new Interval(a.end, b.start));
298+
}
299+
300+
return ans;
301+
}
97302
```
98303

99304
<!-- tabs:end -->

0 commit comments

Comments
(0)

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