竞争性编程简单

活动选择

“活动选择”问题的详细指南和 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 资源

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。