Menu
CoddyTech

Job Sequencing Problem

中等贪心排序python iconjava iconcpp iconc iconjs icon+10

你会得到 n 个任务,它们由两个数组表示:如果任务 i 在时间 deadline[i] 前完成,就能获得 profit[i] 的收益。每个任务需要 1 个时间单位,你从时间 0 开始,每次运行一个任务,也可以跳过任意任务。截止时间为 2 的任务可以在 0 到 1 或 1 到 2 之间运行。

返回你能获得的最大总收益。

函数

jobSequencing(deadline: integer-array, profit: integer-array) → integer
deadlineinteger-array
每项任务必须完成的时间,从 1 到 n
profitinteger-array
每项工作获得的收益;profit[i] 对应 deadline[i]
返回值integer
所有工作都能在各自截止日期前完成时的最大总利润

约束条件

  • 1 ≤ deadline.length == profit.length ≤ 5000
  • 1 ≤ deadline[i] ≤ n,其中 n 是作业数量
  • 1 ≤ profit[i] ≤ 105
  • 这些数组未排序。多个工作可能具有相同的截止日期或利润。

示例

输入
deadline = [3, 1, 2, 3, 1]profit = [40, 35, 30, 25, 50]
输出
120
解释
两个截止时间为 1 的任务中只能安排一个,所以运行价值为 50 的任务。价值为 30 的任务随后在截止时间 2 完成,价值为 40 的任务则在截止时间 3 完成:50 + 30 + 40 = 120。
challenge icon

进阶

如果工作 i 需要 duration[i] 个时间单位,而不是 1 个时间单位呢?优先选择利润最高的工作,仍然能获得最高总利润吗?

重置代码
def jobSequencing(deadline, profit):
    # 在这里编写代码

测试 #1

测试 #2

测试 #3

参数

deadline = [3, 1, 2, 3, 1]
profit = [40, 35, 30, 25, 50]

预期输出

120