Menu
CoddyTech

Job Sequencing Problem

MediumGreedySortingpython iconjava iconcpp iconc iconjs icon+10

You get n jobs as two arrays: job i earns profit[i] if it finishes by time deadline[i]. Each job takes one unit of time, you run one job at a time starting at time 0, and you may skip any job. A job with deadline 2 can run from 0 to 1 or from 1 to 2.

Return the largest total profit you can earn.

Function

jobSequencing(deadline: integer-array, profit: integer-array) → integer
deadlineinteger-array
the time by which each job has to be finished, from 1 to n
profitinteger-array
what each job earns; profit[i] belongs to deadline[i]
Returnsinteger
the largest total profit of jobs that can all finish by their deadlines

Constraints

  • 1 ≤ deadline.length == profit.length ≤ 5000
  • 1 ≤ deadline[i] ≤ n, where n is the number of jobs
  • 1 ≤ profit[i] ≤ 105
  • The arrays are not sorted. Several jobs may share a deadline or a profit.

Examples

Input
deadline = [3, 1, 2, 3, 1]profit = [40, 35, 30, 25, 50]
Output
120
Explanation
Only one of the two deadline-1 jobs fits, so run the one worth 50. The job worth 30 then finishes at its deadline 2 and the job worth 40 at its deadline 3: 50 + 30 + 40 = 120.
challenge icon

Follow-up

What if job i takes duration[i] units of time instead of one? Does taking the most profitable job first still give the best total?

Reset code
def jobSequencing(deadline, profit):
    # Write code here

Test #1

Test #2

Test #3

Arguments

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

Expected Output

120