競賽程式設計簡單

活動選擇

“活動選擇”問題的詳細指南和 Python 實作。

問題陳述

簡單

寫一個函數 activity_selection(start, end),它採用兩個清單 startend 表示活動的開始和結束時間。假設一個人一次只能從事一項活動,求一個人可以執行的最大活動數。如果第二個活動的開始時間大於或等於第一個活動的結束時間,則兩個活動不重疊。

約束條件
  • 1 <= len(start) == len(end) <= 10^5
  • 0 <= start[i] < end[i] <= 10^9

範例

Example 1
Input
activity_selection([1, 3, 0, 5, 8, 5], [2, 4, 6, 7, 9, 9])
Output
4
Explanation

A person can perform at most 4 activities: [1,2], [3,4], [5,7], and [8,9].

Example 2
Input
activity_selection([10, 12, 20], [20, 25, 30])
Output
2
Explanation

Two activities can be performed: [10,20] and [20,30].

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

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