Job Sequencing Problem

Moderate
0/80
Average time to solve is 30m
profile
Contributed by
141 upvotes
Asked in companies
OracleCIS - Cyber InfrastructureExpedia Group

Problem statement

You are given a 'Nx3' 2-D array 'Jobs' describing 'N' jobs where 'Jobs[i][0]' denotes the id of 'i-th' job, 'Jobs[i][1]' denotes the deadline of 'i-th' job, and 'Jobs[i][2]' denotes the profit associated with 'i-th job'.


You will make a particular profit if you complete the job within the deadline associated with it. Each job takes 1 unit of time to be completed, and you can schedule only one job at a particular time.


Return the number of jobs to be done to get maximum profit.


Note :
If a particular job has a deadline 'x', it means that it needs to be completed at any time before 'x'.

Assume that the start time is 0.


For Example :
'N' = 3, Jobs = [[1, 1, 30], [2, 3, 40], [3, 2, 10]].

All the jobs have different deadlines. So we can complete all the jobs.

At time 0-1, Job 1 will complete.
At time 1-2, Job 3 will complete.
At time 2-3, Job 2 will complete.

So our answer is [3 80].
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line contains a single integer 'N', denoting the number of elements of the array 'Jobs'.

Next 'N' lines contain three integers, id, deadline, and profit of the 'ith' job.
Output Format :
The only line contains the number of jobs to be done to get maximum profit and maximum profit.
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
Sample Input 1 :
4
1 2 30
2 2 40
3 1 10
4 1 10
Sample Output 1 :
2 70
Explanation For Sample Input 1 :
At time 0-1, Job 1 will complete.

At time 1-2, Job 2 will complete.

The first and second jobs can be completed within the deadlines, and we earn a profit of 70 by doing so.
Sample Input 2 :
3
1 1 40
2 1 50
3 1 60
Sample Output 2 :
1 60
Constraints :
1 <= 'N' <= 10^5
1 <= jobs[i][0] <= 'N'
1 <= jobs[i][1] <= 'N'
1 <= jobs[i][2] <= 10^3

Time limit: 1 sec
Hint

Think of a greedy approach to complete the jobs with the maximum profit first.

Approaches (2)
Greedy Approach

The idea here is to follow a greedy approach that we should complete the job with the maximum profit in the nearest available slot within its deadline. So, we can sort the jobs in the decreasing order of their profit, and we can maintain a boolean array to keep track of the available slots. So, for each job, we traverse the array backward from the deadline associated with the job to 1, and if any of the slots are empty, we add this profit to the answer and increase our count, else we ignore this job.

 

Algorithm

 

  • Sort the jobs array in descending order of the profit associated with each job. We can write our own comparator function to achieve this.
  • Create the variable, ‘maxProfit’ and ‘numberOfJobs ’, and Initialize it to 0.
  • Create a variable ‘maxDeadline’ and find the maximum available deadline among all the jobs.
  • Create a boolean array named slots of size ‘maxDeadline’ and initialize all the elements to false.
  • Run a loop from i = 0 to N and do:
    • Run a loop j = jobs[i].deadline to 1 and do:
      • If slots[j] == false, then
        • maxProfit = maxProfit + jobs[i].profit
        • numberOfJobs=numberOfJobs + 1.
        • Make slots[j] = true and break out of the loop.
  • Return the numberOfJobs and maxProfit.
Time Complexity

O(N * maxDeadline), where ‘N’ denotes the number of elements of the array “jobs,” and ‘maxDeadline’ is the maximum available deadline among all the jobs.

 

As for every index of the array “jobs”, we may have to search for all the indices of the array “slots”. Hence, the time complexity in the worst case will be O(N * maxDeadline).

Space Complexity

O(maxDeadline), where ‘maxDeadline’ is the maximum available deadline among all the jobs.

 

The slots array/list has a size of maxDeadline.

Video Solution
Unlock at level 3
(75% EXP penalty)
Code Solution
(100% EXP penalty)
Job Sequencing Problem
Full screen
Console