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
• Start time:
• End time:
• Profit:
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
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.
Visualization
Tap to expand
Understanding the Visualization
1
Timeline Setup
Arrange all potential concerts on a timeline, showing their duration and profit
2
Smart Selection
Use binary search to quickly find which previous concerts don't conflict with each new option
3
Profit Maximization
For each concert, decide whether to book it (plus best previous combination) or skip it
Key Takeaway
🎯 Key Insight: By sorting concerts by end time and using binary search to find compatible previous bookings, we can efficiently build the optimal schedule that maximizes venue profit while avoiding conflicts.
Time & Space Complexity
Time Complexity
O(n log n)
O(n log n) for sorting + O(n log n) for binary search in DP loop
⚡ Linearithmic
Space Complexity
O(n)
O(n) space for DP array and sorted jobs array
⚡ Linearithmic Space
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)
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code