↓Skip to main content
  1. LeetCode/

LeetCode 1229: Meeting Scheduler

·2 mins· ·
LeetCode Medium Two-Pointers Sorting Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-02-10

  • Submissions: Wrong Answer ×3 → Accepted ×2

Approach
#

Like LeetCode 986, this intersects two interval lists, except the input here is not sorted.

  1. Sort each person’s slots by start.
  2. Two pointers: the intersection is [max(start1, start2), min(end1, end2)]; if its length is >= duration, return [start, start + duration].
  3. The slot that ends first cannot meet anything else, so advance its pointer.

The pointers visit intersections in time order, so the first one long enough is the earliest.

My Attempts
#

VersionProblem
Attempt 1Hard-coded the length as + 8 (the example’s duration), compared length with >, and did not sort. For [[0, 10]], [[0, 10]], duration = 3, the answer is [0, 3]; it returned [0, 8]
Attempt 2Switched to + duration but still used >, skipping intersections exactly duration long. For [[0, 5]], [[0, 5]], duration = 5, the answer is [0, 5]; it returned []
Attempt 3Switched to >= but still did not sort. For slots1 = [[10, 15], [0, 5]], slots2 = [[0, 5]], duration = 2, the answer is [0, 2]; this compared [10, 15] with slots2’s [0, 5], found no overlap, skipped [0, 5] because it ends first, and returned []

Adding the sort passed. I later removed the “skip slots that are too short” loops, since the intersection length check already covers them.

Solution
#

class Solution:
    def minAvailableDuration(self, slots1: List[List[int]], slots2: List[List[int]], duration: int) -> List[int]:
        slots1.sort()
        slots2.sort()
        i = j = 0
        while i < len(slots1) and j < len(slots2):
            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                             # slots1[i] ends first
            else:
                j += 1
        return []

Complexity
#

Time O(n log n + m log m), space O(1) apart from sorting.

Mistakes to Avoid
#

  • Unsorted input: the problem only guarantees one person’s slots don’t overlap, not their order.
  • A length exactly equal to duration works, so use >=.
  • Hard-coding numbers from the example.

Interview Explanation
#

This is interval intersection with unsorted input, so I sort both people’s slots and use two pointers to find intersections, returning the start plus duration once one is long enough. The pointers visit intersections in time order, so the first one found is the earliest. The slot that ends first advances. Sorting dominates at O(n log n).

Related

LeetCode 1471: The k Strongest Values in an Array
·2 mins
LeetCode Medium Two-Pointers Sorting Array
LeetCode 1471 note: after sorting, the value farthest from the median is always at one of the ends, so converging pointers pick the stronger end k times
LeetCode 2563: Count the Number of Fair Pairs
·3 mins
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 2563 note: split “sum in [lower, upper]” into “sum <= upper” minus “sum <= lower - 1”, and count each with converging pointers over the sorted array in O(n)
LeetCode 1498: Number of Subsequences That Satisfy the Given Sum Condition
·2 mins
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 1498 note: only the minimum and maximum matter, so order does not and we can sort; fixing nums[left] as the minimum, the elements between are free, giving 2^(right - left) choices
LeetCode 923: 3Sum With Multiplicity
·4 mins
LeetCode Medium Two-Pointers Sorting Hash-Table Counting Array
LeetCode 923 note: sort, fix the first number, and find the other two with converging pointers; equal end values are counted with a combination, different ones by multiplying each end’s duplicate count
LeetCode 948: Bag of Tokens
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 948 note: after sorting, buy score with the cheapest token and buy power with the most expensive one; trade for power only when score > 0, and stop when neither move works
LeetCode 825: Friends Of Appropriate Ages
·3 mins
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 825 note: after sorting, the people someone can invite have ages in (0.5 × age + 7, age], a contiguous range kept with two same-direction pointers, minus the person themself