Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Scheduling jobs means that ordering them according to their start and end days in such a way that no two jobs are scheduled at the same time/day. If there is some profit associated with the jobs, we can find the maximum profit that can be earned by scheduling them most optimally.
Let’s understand the problem statement in depth to get a better understanding.
Problem Statement
They are given n jobs with their starting day, ending day, and their profit. Schedule the jobs so that only one job is done on one day, and the profit earned by scheduling them is the maximum. Return the maximum profit.
Note: You will be devoted to one job from its start day to end day, i.e., you can’t start any other task in between.
For example:
INPUT : n = 4
2 4 4
3 6 6
6 8 2
5 7 3
OUTPUT: 7
Explanation: Job 1 and Job 2 will coincide. Job 2 and Job 3 will again coincide on day 6. Job 3 and Job 4 will also coincide. But Job 1 and Job 4 won’t coincide and also give a profit of 7.
The most brute idea is to try out all the subsets and find the maximum profit among them. But this will have an exponential time complexity which is, we’re sure, not required here.
We know that the set of jobs, let’s say S, that will give the maximum profit is undoubtedly a subset of the assigned n jobs. If we talk about some nth job, this nth job will either be in the set S or not.
Now, if the nth job is not in S, then our search space kind of gets reduced till n-1 only, and now we’ve to find the answer for the jobs 1 to n-1 only.
1,2,3,4,.....,n get reduced to 1,2,3,....,n-1 as n doesn’t contribute to the answer. Therefore, it can be neglected.
If the nth job is in S, then we know that all jobs whose end days are greater than or equal to the start day of the nth job can’t be there in S. So, now the next job included will be the largest i from the 1(let’s say j) which has end[j] <start[n]. In this case, now our search space gets reduced till j. And the total answer will be the (answer of jobs from 1 to j ) + profit[n].
So the discussion reduces to how to find that particular j?
What we can do is, find all the end days in a sorted vector and find it using the lower_bound function on that vector.
So, you can see that if we define dp[i] as the maximum possible profit from tasks 1 to i, the problem is solved. dp[0] will be 0 because no profit can be made from 0 tasks.
The solution involves a technique called dynamic programming. So, it’s suggested that you revise this topic once
Steps are:
Make a struct of jobs containing data of start day as “strt,” end day as “ed,” and profit as “profit.”
Declare a vector v of jobs of size n+1. Here, the size is n+1 because we’ll do the indexing starting from 1.
Take the n inputs and store them in the vector v.
Sort the vector v using a comparator that sorts the jobs in ascending order according to their end days.
Declare the dp array of size n+1 and update dp[0] = 0;
Declare a vector ends and push all the ends in this vector. It will be a sorted vector because we’re pushing from vector v, which is itself sorted according to the end days.
Traverse from 1 to n.
If we don’t include/take the job in S, dp[i] depends on dp[i-1].
If we include/take it, it depends on dp[j] such that j is the largest index from 1 which has v[i].ed<v[i].strt. In this case, dp[i] = profit[i]+dp[j]. We’ll find this j using the findj() function which basically searches j using the lower_bound() function on ends vector for value v[i].strt.
intfindj(vector<int>&ends, int strtofi){ auto it = lower_bound(ends.begin(),ends.end(),strtofi); if(it==ends.begin()){ return0; } else{ it--; return1+(it-ends.begin()); } }
intmain(){ int n; cin>>n; vector<job>v(n+1); for(int i=1;i<n+1;i++){ cin>>v[i].strt>>v[i].ed>>v[i].profit; } sort(v.begin()+1,v.end(),comp); int dp[n+1]; dp[0] = 0; vector<int>ends; for(int i=1;i<v.size();i++){ ends.push_back(v[i].ed); } for(int i=1;i<n;i++){ int take = dp[i-1]; int nottake = v[i].profit; int j = findj(ends,v[i].strt); nottake+=dp[j]; dp[i] = max(take,nottake); } cout<<dp[n-1]<<endl; return0; }
Input
4
2 4 4
3 6 6
6 8 2
5 7 3
Output
7
Time complexity
O(nlogn), where n is the number of jobs.
Reason: Because we’re iterating through i=1 to i<n (which contributes O(n)) and O(logn) for finding j using the lower_bound() function, and it works based on binary search.
Space complexity
O(n), where n is the number of jobs.
Reason: We’re storing the values of dp for each i from 0 to n-1.
What is dynamic programming, and where is it used?
Answer: Dynamic programming is an optimization method used in various programming problems. It is used in problems where the solution depends on smaller overlapping subproblems. We use it to memorize the results so that they can easily be used later when needed.
Answer: A problem in which we’ve to find the maximum profit earned by scheduling jobs such that only one job is done on one day is called the weighted job scheduling problem.
What are overlapping subproblems?
Answer: A problem has overlapping subproblems if it can be divided into smaller subproblems that are reused multiple times.
Does dynamic programming have overlapping subproblems?
Answer: Yes, dynamic programming is only used in problems where there are overlapping subproblems.
Where can I submit my “Maximum profit in job scheduling” code?
Answer: You can submit your code on Coding Ninjas Studio and get it accepted here right away.
Are there more Data Structures and Algorithms problems in Coding Ninjas Studio?
Answer: Yes, Coding Ninjas Studio is a platform that provides both practice coding questions and commonly asked interview questions. The more we’ll practice, the better our chances are of getting into a dream company of ours.
Key takeaways
In this article, we’ve discussed the maximum profit in the job scheduling problem. Here an effective method has been used, which is called dynamic programming. This is a crucial topic, and there are numerous exciting problems related to this topic. Some of these are
I would suggest you solve them to gain more confidence on this topic. These questions are asked during various coding contests as well as placements tests.
To practice more such problems, Coding Ninjas Studio is a one-stop destination. This platform will help you acquire effective coding techniques and give you an overview of student interview experience in various product-based companies.
Happy Coding!
Live masterclass
System Design Questions Asked at Microsoft, Oracle, PayPal
by Pranav Malik
23 Apr, 2025
01:30 PM
Master DSA to Ace MAANG SDE Interviews
by Saurav Prateek
21 Apr, 2025
01:30 PM
Google Data Analyst roadmap: Essential SQL concepts
by Maaheen Jaiswal
22 Apr, 2025
01:30 PM
Amazon Data Analyst: Advanced Excel & AI Interview Tips
by Megna Roy
24 Apr, 2025
01:30 PM
System Design Questions Asked at Microsoft, Oracle, PayPal