Maximum Profit in Job Scheduling - Problem
Job Scheduling for Maximum Profit

Imagine you're a freelance consultant with multiple job offers, but you can't work on overlapping projects. You have n jobs available, where each job i has:

Start time: startTime[i]
End time: endTime[i]
Profit: profit[i]

Your goal is to maximize your total earnings by selecting a subset of non-overlapping jobs. Note that if a job ends at time X, you can immediately start another job that begins at time X.

Example: Jobs [(1,3,$20), (2,5,$30), (4,6,$70)] → Choose jobs 1 and 3 for profit $90

Input & Output

example_1.py — Basic Case
$ Input: startTime = [1,2,3,3], endTime = [3,4,5,6], profit = [50,10,40,70]
Output: 120
💡 Note: Select jobs 1 (1-3, profit 50) and job 4 (3-6, profit 70). Total profit = 50 + 70 = 120. Job 3 can't be selected because it overlaps with both selected jobs.
example_2.py — No Overlaps
$ Input: startTime = [1,2,3,4,6], endTime = [3,5,10,6,9], profit = [20,20,100,70,60]
Output: 150
💡 Note: Select jobs 1 (1-3, profit 20), 4 (4-6, profit 70), and 5 (6-9, profit 60). These don't overlap: 1-3, 4-6, 6-9. Total = 20 + 70 + 60 = 150.
example_3.py — Single Job
$ Input: startTime = [1], endTime = [2], profit = [50]
Output: 50
💡 Note: Only one job available, so select it for profit 50.

Constraints

  • 1 ≤ startTime.length == endTime.length == profit.length ≤ 5 * 104
  • 1 ≤ startTime[i] < endTime[i] ≤ 109
  • 1 ≤ profit[i] ≤ 104
  • Jobs can start immediately when another ends (endTime[i] == startTime[j] is valid)

Visualization

Tap to expand
Maximum Profit in Job Scheduling INPUT 0 1 2 3 4 5 6 $50 J1 $10 J2 $40 J3 $70 J4 startTime = [1,2,3,3] endTime = [3,4,5,6] profit = [50,10,40,70] Jobs Summary Job Start End Profit J1 1 3 $50 J2 2 4 $10 J3 3 5 $40 J4 3 6 $70 ALGORITHM STEPS 1 Sort by End Time Order jobs: J1, J2, J3, J4 2 Initialize DP Array dp[i] = max profit up to job i 3 Binary Search Find last non-overlapping job 4 DP Transition dp[i] = max(skip, take) DP Table Job End Prev Take DP J1 3 - 50 50 J2 4 - 10 50 J3 5 J1 90 90 J4 6 J1 120 120 FINAL RESULT Optimal Selection 0 1 2 3 4 5 6 Job 1: $50 Job 4: $70 J2 (skip) J3 (skip) Profit Calculation Job 1: $50 Job 4: $70 Maximum Profit $120 OK Key Insight: Dynamic Programming with Binary Search: Sort jobs by end time, then for each job decide to either SKIP it (keep previous max) or TAKE it (profit + best compatible previous job). Use binary search to find the last non-overlapping job in O(log n). Total complexity: O(n log n). TutorialsPoint - Maximum Profit in Job Scheduling | Dynamic Programming Approach Approach: DP + Binary Search
Asked in
Google 42 Amazon 38 Microsoft 31 Meta 25
73.9K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen