Weighted Job Scheduling
Expert Answer & Key Takeaways
A complete guide to understanding and implementing Greedy.
Weighted Job Scheduling
Find maximum profit from non-overlapping jobs with given
startTime, endTime, and profit.Visual Representation
Jobs (S, E, P):
J1: [1, 3, 50], J2: [2, 4, 10], J3: [3, 5, 40], J4: [3, 6, 70]
Combination J1 (50) + J4 (70) = 120 (Max)Examples
Input: startTime = [1,2,3,3], endTime = [3,4,5,6], profit = [50,10,40,70]
Output: 120
The subset of jobs [J1, J4] gives maximum profit.
Approach 1
Level I: Recursive Backtracking
Intuition
Try all possible subsets of compatible jobs and return the maximum profit. To handle compatibility, we sort by start time and at each step, decide to either pick the current job and skip all overlapping ones, or skip the current job.
Thought Process
solve(idx): Max profit starting from jobidx.- Decision 1: Skip job
idx->solve(idx + 1). - Decision 2: Pick job
idx. Find the next jobk > idxthat starts afterjobs[idx].end. Result =profit[idx] + solve(k). - Return
max(Decision 1, Decision 2).
⏱ O(2^N).💾 O(N)
Detailed Dry Run
Jobs: [1,3,50], [2,4,10], [3,5,40]
- solve(0):
- Skip J1: solve(1)
- Pick J1: 50 + solve(2) (J3 starts at 3, J1 ends at 3). Result = 50 + 40 = 90.
- Return 90.
Approach 2
Level II: Dynamic Programming (O(N^2))
Intuition
This is a variation of the Longest Increasing Subsequence (LIS) problem. For each job
i, we look at all previous jobs j < i and if they are compatible, we update dp[i] = max(dp[i], dp[j] + profit[i]).Thought Process
- Sort jobs by end time.
dp[i]= max profit ending at jobi.- For each
i, iterate allj < iand check ifjobs[j].end <= jobs[i].start. - This approach is and will pass for medium constraints but TLE for .
⏱ O(N^2).💾 O(N).
Detailed Dry Run
Jobs: [J1:1-3, 50], [J2:2-4, 10], [J3:3-5, 40]
- dp[0] = 50 (J1)
- dp[1] = 10 (J2). J1 overlaps with J2.
- dp[2] = 40 + dp[0] = 90. J1 is compatible with J3. Max Profit = 90.
Approach 3
Level III: Optimal (DP + Binary Search)
Intuition
Sort by end time. For each job, the max profit is either excluding it (same as max profit of previous job) or including it (profit + max profit of latest compatible job).
Thought Process
- Combine data and sort by
endTime. dp[i]= max profit for firstijobs.- Search for the latest job
j < iwherejobs[j].end <= jobs[i].startusing binary search. dp[i] = max(dp[i-1], job[i].profit + dp[j]).
Pattern: Dynamic Programming + Binary Search
⏱ O(N log N) for sorting and binary searching.💾 O(N) for DP array.
Detailed Dry Run
Sorted: [J1:[1,3,50], J2:[2,4,10], J3:[3,5,40], J4:[3,6,70]]
J1: dp[0]=50
J2: j=-1, dp[1]=max(50, 10)=50
J3: j=0, dp[2]=max(50, 40+50)=90
J4: j=0, dp[3]=max(90, 70+50)=120
Course4All Technical Board
Verified ExpertSenior Software Engineers & Algorithmic Experts
Our DSA content is authored and reviewed by engineers from top tech firms to ensure optimal time and space complexity analysis.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.