↓快轉到主要內容
  1. LeetCode/

LeetCode 1229: Meeting Scheduler

·1 分鐘· ·
LeetCode Medium Two-Pointers Sorting Array
Wei Yi Chung
作者
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
目錄

基本資料
#

難易度: medium 第一次嘗試:2026-02-10

  • 提交紀錄:Wrong Answer ×3 → Accepted ×2

解題思路
#

跟 LeetCode 986 一樣是兩個區間列表找交集,差別是這題的輸入沒有排序。

  1. 兩個人的時段各自依起點排序。
  2. 雙指針:交集是 [max(start1, start2), min(end1, end2)],長度 >= duration 就回傳 [start, start + duration]。
  3. 結束得比較早的那個時段不會再跟別人相交,移動它的指針。

雙指針是依時間順序走過所有交集,所以第一個夠長的交集就是最早的。

我的嘗試
#

版本問題
第一版回傳時把長度寫死成 + 8(範例的 duration),長度判斷用 >,也沒排序。例如 [[0, 10]]、[[0, 10]]、duration = 3:答案是 [0, 3],回傳 [0, 8]
第二版改成 + duration,但還是 >,剛好等於 duration 的交集被跳過。例如 [[0, 5]]、[[0, 5]]、duration = 5:答案是 [0, 5],回傳 []
第三版改成 >=,但沒排序。例如 slots1 = [[10, 15], [0, 5]]、slots2 = [[0, 5]]、duration = 2:答案是 [0, 2],這版拿 [10, 15] 跟 slots2 的 [0, 5] 比,沒有交集,[0, 5] 比較早結束就被跳過了,回傳 []

補上排序就 AC 了。之後又把「先跳過太短的時段」那段拿掉,因為交集的長度判斷已經涵蓋它。

正確解法
#

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] 先結束
            else:
                j += 1
        return []

複雜度
#

時間 O(n log n + m log m),空間 O(1)(不算排序)。

要特別避免的錯誤
#

  • 輸入沒排序:題目只保證同一個人的時段不重疊,沒保證順序。
  • 長度剛好等於 duration 也可以,要用 >=。
  • 把範例的數字寫死在程式裡。

面試口說整理
#

這題跟區間交集一樣,只是輸入沒排序,所以我先把兩個人的時段排序,再用雙指針找交集,交集長度夠就回傳起點加 duration。雙指針是依時間順序走過交集,所以第一個找到的就是最早的。結束比較早的時段先移動。時間由排序決定,O(n log n)。

相關文章

LeetCode 1471: The k Strongest Values in an Array
·1 分鐘
LeetCode Medium Two-Pointers Sorting Array
LeetCode 1471 解題筆記:排序後離中位數最遠的一定在兩端,對撞指針每次從兩端挑比較強的那個,挑 k 次
LeetCode 2563: Count the Number of Fair Pairs
·2 分鐘
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 2563 解題筆記:把「和落在 [lower, upper]」拆成「和 <= upper」減「和 <= lower - 1」,每個都用排序後的對撞指針 O(n) 數
LeetCode 1498: Number of Subsequences That Satisfy the Given Sum Condition
·2 分鐘
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 1498 解題筆記:子序列只看最小值和最大值,排序後順序無關;固定最小值 nums[left],中間的元素任意選,共 2^(right - left) 種
LeetCode 923: 3Sum With Multiplicity
·3 分鐘
LeetCode Medium Two-Pointers Sorting Hash-Table Counting Array
LeetCode 923 解題筆記:排序後固定第一個數、對撞指針找另外兩個;兩端值相同時用組合數一次算完,不同時把兩端各自的重複數相乘
LeetCode 948: Bag of Tokens
·1 分鐘
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 948 解題筆記:排序後,用最小的 token 換分數、用最大的 token 換能量;只有分數 > 0 才能換能量,換不了就停
LeetCode 825: Friends Of Appropriate Ages
·2 分鐘
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 825 解題筆記:排序後,每個人能送出邀請的對象是年齡落在 (0.5 × age + 7, age] 的一段區間,用兩個同向指針維護區間再扣掉自己