-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path3653.meeting-scheduler.py
126 lines (114 loc) · 3.39 KB
/
3653.meeting-scheduler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Tag: Same Direction Two Pointers, Two Pointers, Sort, Line Sweep
# Time: O(N + M)
# Space: O(1)
# Ref: Leetcode-1229
# Note: -
# Given the availability schedules `slots1` and `slots2` of two people, and the expected `duration` of the meeting, you are asked to schedule **the earliest and appropriate** meeting time in the interval for them.
#
# The format of an **availability schedules** is an ***Interval*** consisting of a `start` time and an `end` time, i.e., `(start, end)`, which means that it starts at `start` and ends at `end`.
#
#
# If the required meeting time is not met, return to the interval `(-1, -1)`.
#
# **Example 1**
#
# Input
# ```
# slots1 = [(10,50),(60,120),(140,210)]
# slots2 = [(0,15),(60,70)]
# duration = 8
# ```
#
# Output
# ```
# (60,68)
# ```
#
# **Example 2**
#
# Input
# ```
# slots1 = [(10,50),(60,120),(140,210)]
# slots2 = [(0,15),(60,70)]
# duration = 12
# ```
#
# Output
# ```
# (-1,-1)
# ```
#
# $1 <= slots1.length, slots2.length <= 10^4$
#
# $slots1[i].length, slots2[i].length == 2$
#
# $slots1[i].start < slots1[i].end$
#
# $slots2[i].start < slots2[i].end$
#
# $0 <= slots1[i][j], slots2[i][j] <= 10^9$
#
# $1 <= duration <= 10^6$
from typing import (
List,
)
from lintcode import (
Interval,
)
"""
Definition of Interval:
class Interval(object):
def __init__(self, start, end):
self.start = start
self.end = end
"""
class Solution:
"""
@param slots1: The availability schedule of one.
@param slots2: The availability schedule of one.
@param duration: The expected duration of the meeting.
@return: The earliest and appropriate meeting time in the interval for them.
"""
def earliest_appropriate_duration(self, slots1: List[Interval], slots2: List[Interval], duration: int) -> Interval:
# --- write your code here ---
i = 0
j = 0
while i < len(slots1) and j < len(slots2):
tmp = [max(slots1[i].start, slots2[j].start), min(slots1[i].end, slots2[j].end)]
if tmp[1] - tmp[0] >= duration:
return Interval(tmp[0], tmp[0] + duration)
if slots1[i].end >= slots2[j].end:
j += 1
else:
i += 1
return Interval(-1, -1)
from collections import defaultdict
class Solution:
"""
@param slots1: The availability schedule of one.
@param slots2: The availability schedule of one.
@param duration: The expected duration of the meeting.
@return: The earliest and appropriate meeting time in the interval for them.
"""
def earliest_appropriate_duration(self, slots1: List[Interval], slots2: List[Interval], duration: int) -> Interval:
# --- write your code here ---
lines = defaultdict(int)
for interval in slots1:
lines[interval.start] += 1
lines[interval.end] -= 1
for interval in slots2:
lines[interval.start] += 1
lines[interval.end] -= 1
prefix = 0
begin = False
start = 0
for t in sorted(lines.keys()):
prefix += lines[t]
if prefix == 2:
begin = True
start = t
if prefix < 2 and begin:
if t - start >= duration:
return Interval(start, start + duration)
begin = False
return Interval(-1, -1)