Meeting Scheduler — LeetCode 1229 Python Solution
- Problem
- #1229
- Pattern
- Two Pointers
- Reading time
- 3 min
- Source
- leetcode.com
The problem
Given the availability time slots arrays slots1 and slots2 of two people and a meeting duration duration, return the earliest time slot that works for both of them and is of duration duration. If there is no common time slot that satisfies the requirements, return an empty array.
Example
- Input
- slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 8
- Output
- [60,68]
Python solution
class Solution:
def minAvailableDuration(
self, slots1: List[List[int]], slots2: List[List[int]], duration: int
) -> List[int]:
slots1.sort()
slots2.sort()
m, n = len(slots1), len(slots2)
i = j = 0
while i < m and j < n:
start = max(slots1[i][0], slots2[j][0])
end = min(slots1[i][1], slots2[j][1])
if end - start >= duration:
return [start, start + duration]
if slots1[i][1] < slots2[j][1]:
i += 1
else:
j += 1
return []Complexity
| Measure | Complexity |
|---|---|
| Time | O(m \times \log m + n \times \log n) |
| Space | O(\log m + \log n) auxiliary |
Pattern: Two Pointers
Use the order already in the input to discard half the search space at every step. LeetCode 1229. Meeting Scheduler is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Two Pointers.
The two pointers guide has the Python template for the pattern and the 201 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1229. Meeting Scheduler?
- LeetCode 1229. Meeting Scheduler is rated Medium on LeetCode.
- What is the time complexity of LeetCode 1229. Meeting Scheduler?
- The Python solution on this page runs in O(m \times \log m + n \times \log n).
- What is the space complexity of LeetCode 1229. Meeting Scheduler?
- The Python solution on this page uses O(\log m + \log n) auxiliary space.
- What topics does LeetCode 1229. Meeting Scheduler cover?
- LeetCode 1229. Meeting Scheduler is tagged Array, Two Pointers and Sorting on LeetCode.
- Is LeetCode 1229. Meeting Scheduler a premium problem?
- Yes. LeetCode 1229. Meeting Scheduler is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.