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

LeetCode 4026: Maximum Gap Between Stations

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

基本資料
#

難易度: medium 第一次嘗試:2026-08-26

  • 提交紀錄:Wrong Answer → Accepted

解題思路
#

工人要依序配到 skill[i] == station[j] 而且 index 嚴格遞增的站,就是在 station 裡找 skill 這個子序列。

要讓第 i 和第 i + 1 個工人之間的間隔最大:

  • 工人 0..i 盡量靠左:從左邊貪心配子序列,得到每個工人最早可用的站 earliest[i]。
  • 工人 i + 1..n - 1 盡量靠右:從右邊貪心配,得到最晚可用的站 latest[i + 1]。

兩邊互不干擾(earliest[i] < latest[i + 1],因為題目保證有合法的配法),所以間隔 latest[i + 1] - earliest[i] 一定做得到。對每個 i 取最大值就是答案。

兩次貪心配對都是在 skill 和 station 上走兩個同向指針,跟判斷子序列一樣。

我的嘗試
#

第一版從右邊配的時候,是從最後一個工人往前 append,所以 latest 是反過來的,卻直接拿 latest[i + 1] 用。例如 skill = "ab"、station = "aabb":最早的 a 在 0、最晚的 b 在 3,答案是 3;這版回傳 1。

補上 right.reverse() 就 AC 了。

正確解法
#

class Solution:
    def maximumGap(self, skill: str, station: str) -> int:
        n, m = len(skill), len(station)
        earliest = [0] * n
        j = 0
        for i in range(n):
            while station[j] != skill[i]:
                j += 1
            earliest[i] = j                        # 工人 i 最早能用的站
            j += 1

        latest = [0] * n
        j = m - 1
        for i in range(n - 1, -1, -1):
            while station[j] != skill[i]:
                j -= 1
            latest[i] = j                          # 工人 i 最晚能用的站
            j -= 1

        return max((latest[i + 1] - earliest[i] for i in range(n - 1)), default=0)

直接用 index 寫入 latest[i],就不用再反轉。只有一個工人時 default=0。

複雜度
#

時間 O(n + m),空間 O(n)。

要特別避免的錯誤
#

  • 從右邊往前 append 的列表是反的。
  • 只有一個工人時答案是 0。

面試口說整理
#

工人配站就是在 station 裡找 skill 的子序列。要讓某兩個相鄰工人的間隔最大,前面的工人盡量靠左、後面的工人盡量靠右。我從左邊貪心配出每個工人最早的站、從右邊配出最晚的站,在每個切點算 latest[i + 1] - earliest[i] 取最大值。兩次都是同向雙指針,時間 O(n + m)。

相關文章

LeetCode 1616: Split Two Strings to Make Palindrome
·2 分鐘
LeetCode Medium Two-Pointers String Greedy
LeetCode 1616 解題筆記:外側用 a 的前綴配 b 的後綴貪心比對,配不下去時,剩下的中間段必須整段在 a 或 b 裡是回文;兩個方向都要檢查
LeetCode 244: Shortest Word Distance II
·1 分鐘
LeetCode Medium Two-Pointers Design Hash-Table String Array
LeetCode 244 解題筆記:預處理每個字的出現位置(天然排序),查詢時對兩個位置列表做雙指針合併,每次移動比較小的那個
LeetCode 2337: Move Pieces to Obtain a String
·1 分鐘
LeetCode Medium Two-Pointers String
LeetCode 2337 解題筆記:跳過空格後兩邊的棋子順序必須一樣;L 只能往左、R 只能往右,所以每顆 L 在 start 的位置要 >= target,R 要 <=
LeetCode 777: Swap Adjacent in LR String
·1 分鐘
LeetCode Medium Two-Pointers String
LeetCode 777 解題筆記:XL→LX 是 L 往左、RX→XR 是 R 往右;跳過 X 後順序要一樣,L 只能往左、R 只能往右
LeetCode 809: Expressive Words
·2 分鐘
LeetCode Medium Two-Pointers String Array
LeetCode 809 解題筆記:兩個指針一段一段比對連續相同字元的長度;長度不同時,s 的那段必須至少 3 個且不少於 word;最後兩個字串都要剛好走完
LeetCode 1989: Maximum Number of People That Can Be Caught in Tag
·1 分鐘
LeetCode Medium Two-Pointers Greedy Array
LeetCode 1989 解題筆記:鬼從左到右,每個鬼抓範圍內最左邊、還沒被抓的人;用一個只往右走的指針指向下一個候選人