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
🎭 Concert Venue Optimal SchedulingAvailable Concerts:Rock Band1-3pm, $50kJazz Trio2-5pm, $30kPop Star4-6pm, $70kTimeline Analysis:Time: 12 1 2 3 4 5 6 7Rock $50kJazz $30kPop $70kDP Decision Process:Step 1:Sort by end time: Rock(1-3) → Pop(4-6) → Jazz(2-5)Step 2:Rock: Take it (profit = $50k, total = $50k)Step 3:Pop: Compatible with Rock! (profit = $70k + $50k = $120k)Step 4:Jazz: Conflicts with both → Skip (keep $120k)🏆 OPTIMAL BOOKING$120,000Rock Band + Pop Star
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.
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