Meeting Scheduler — LeetCode 1229 Python Solution

MediumLeetCode PremiumArrayTwo PointersSorting
Problem
#1229
Reading time
3 min

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

Python
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

MeasureComplexity
TimeO(m \times \log m + n \times \log n)
SpaceO(\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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview