競賽程式設計簡單

作業排序

“作業排序”問題的詳細指南和 Python 實作。

問題陳述

簡單

寫一個函數 job_scheduling(jobs),它接受元組列表 jobs,其中每個元組是 (job_id, deadline, profit)。每項工作需要 1 個單位時間才能完成。找出最大利潤和完成的工作數量,並將它們作為元組 (job_count, max_profit) 返回。

約束條件
  • 1 <= len(jobs) <= 10^4
  • 1 <= deadline <= 100
  • 1 <= profit <= 1000

範例

Example 1
Input
job_scheduling([(1, 4, 20), (2, 1, 10), (3, 1, 40), (4, 1, 30)])
Output
(2, 60)
Explanation

We can do job 3 at time 1 and job 1 at time 2. Total profit is 40 + 20 = 60.

Example 2
Input
job_scheduling([(1, 2, 100), (2, 1, 19), (3, 2, 27), (4, 1, 25), (5, 3, 15)])
Output
(3, 142)
Explanation

We can do job 1, 3, and 5 for a total profit of 100 + 27 + 15 = 142.

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 資源

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