快轉到主要內容
  1. LeetCode/

LeetCode 853: Car Fleet

·1 分鐘· ·
LeetCode Daily Medium Stack Sorting Simulation Greedy Array 堆疊 排序 模擬 貪心 陣列
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 Stack Monotonic-Stack Array Temperature Waiting-Time Algorithm Data-Structure 溫度 堆疊 陣列 等待時間 演算法
使用單調堆疊解決每日溫度問題
LeetCode 150: Evaluate Reverse Polish Notation
·1 分鐘
LeetCode Daily Medium Stack Expression-Evaluation Reverse-Polish-Notation Postfix-Notation Mathematics Algorithm Data-Structure 逆波蘭表示法 堆疊 表達式求值 數學 演算法
使用堆疊解決逆波蘭表示法求值問題
LeetCode 84: Largest Rectangle in Histogram
·1 分鐘
LeetCode Daily Hard Stack Monotonic-Stack Array Histogram Geometry Algorithm Dynamic-Programming 直方圖 堆疊 幾何 演算法
使用單調堆疊解決直方圖中最大矩形面積問題
LeetCode 901: Online Stock Span
·1 分鐘
LeetCode Daily Medium Stack Monotonic-Stack Design Data-Structure Algorithm 股票 堆疊 設計模式
使用單調堆疊解決股票價格跨度問題
LeetCode 503: Next Greater Element II(單調棧)
·1 分鐘
LeetCode Daily Medium Monotonic-Stack Stack Array Circular-Array 單調棧 環狀陣列
單調棧、環狀陣列
LeetCode 155: Min Stack
·1 分鐘
LeetCode Daily Medium Stack Design Data-Structure Tuple Constant-Time 堆疊 設計 資料結構 元組 常數時間
使用元組方法實現具有 O(1) 最小值檢索的堆疊