150強訪談簡單

任務調度程序

“任務計劃程序”問題的詳細指南和 Python 實作。

問題陳述

簡單

給定一個 CPU 任務數組,每個任務都由字母 A 到 Z 和冷卻間隔 n 表示。每個週期或間隔允許完成一項任務。任務可以按任何順序完成,但有一個限制:由於冷卻要求,相同的任務必須至少間隔 n 個間隔。

傳回完成所有任務所需的最小間隔數。

寫一個函數 leastInterval(tasks: List[str], n: int) -> int

約束條件
  • 1 <= len(tasks) <= 10^4
  • tasks[i] is an uppercase English letter
  • 0 <= n <= 100

範例

Example 1
Input
tasks = ["A","A","A","B","B","B"], n = 2
Output
8
Explanation

A possible sequence is A -> B -> idle -> A -> B -> idle -> A -> B.

Example 2
Input
tasks = ["A","A","A","B","B","B"], n = 0
Output
6
Explanation

With no cooling interval, tasks can be executed continuously without idles.

Need a Hint?
考慮使用堆/優先權佇列特定的資料結構,例如集合或堆。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在編輯器中開啟
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。