快轉到主要內容
  1. LeetCode/

LeetCode 853: Car Fleet

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

基本資料
#

難易度: Medium 第一次嘗試: 2025-08-10

  • 總花費時間:00:00.00

解題思路
#

每輛車都有到達目標位置的時間。我們可以按照車輛位置排序,從距離目標最近的車開始遍歷到最遠的車。如果一輛車的到達時間小於或等於堆疊中最後一輛車的到達時間,就表示這輛車會追上並與前面的車形成車隊。

解法
#

這個解決方案使用堆疊方法,包含以下步驟:

  1. 計算到達時間:對於每輛車,使用 (target - position) / speed 計算到達目標所需的時間
  2. 按位置排序:按照車輛位置降序排列(距離目標最近的車優先)
  3. 堆疊處理:使用堆疊來追蹤形成車隊的車輛
  4. 車隊形成:如果一輛車能追上前面的車(到達時間 ≤ 前車到達時間),它就加入車隊
class Solution:
    def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
        position_rate = [(position[i], (target - position[i]) / speed[i]) for i in range(len(position))]
        position_rate.sort(key=lambda x: (x[0], -x[1]))
        stack = []

        for position, time_to_end in position_rate:
            while stack and stack[-1][1] <= time_to_end: # 表示會追上
                stack.pop()
            
            stack.append((position, time_to_end))

        return len(stack)

時間複雜度:O(n log n),主要來自排序 空間複雜度:O(n),用於堆疊儲存

收穫
#

  • 堆疊方法:使用堆疊來追蹤車隊既有效率又直觀
  • 位置排序:按照距離目標的遠近排序車輛,簡化了車隊形成的邏輯
  • 追趕條件:關鍵洞察是如果一輛車的到達時間 ≤ 前車到達時間,它們就形成車隊
  • 貪心特性:這個問題展示了貪心方法,我們按順序處理車輛並在每一步做出最佳決策

遇到的問題
#

相關文章

LeetCode 739: Daily Temperatures
·1 分鐘
LeetCode Daily Medium Array Stack Monotonic-Stack Data-Structures Temperature Waiting-Time
使用單調堆疊解決每日溫度問題
LeetCode 503: Next Greater Element II(單調棧)
·1 分鐘
LeetCode Daily Medium Array Stack Monotonic-Stack Circular-Array
單調棧、環狀陣列
LeetCode 150: Evaluate Reverse Polish Notation
·1 分鐘
LeetCode Daily Medium Stack Data-Structures Expression-Evaluation Math Postfix-Notation Reverse-Polish-Notation
使用堆疊解決逆波蘭表示法求值問題
LeetCode 155: Min Stack
·1 分鐘
LeetCode Daily Medium Stack Constant-Time Data-Structures Design Tuple
使用元組方法實現具有 O(1) 最小值檢索的堆疊
LeetCode 84: Largest Rectangle in Histogram
·1 分鐘
LeetCode Daily Hard Array Stack Monotonic-Stack Dynamic-Programming Geometry Histogram
使用單調堆疊解決直方圖中最大矩形面積問題
LeetCode 901: Online Stock Span
·1 分鐘
LeetCode Daily Medium Stack Monotonic-Stack Data-Structures Design Design-Patterns Stock
使用單調堆疊解決股票價格跨度問題