We have n jobs, where every job is scheduled to be done from startTime[i] to endTime[i], obtaining a profit of profit[i].
You're given the startTime, endTime and profit arrays, return the maximum profit you can take such that there are no two jobs in the subset with overlapping time range.
If you choose a job that ends at time X you will be able to start another job that starts at time X.
Example 1:

Input: startTime = [1,2,3,3], endTime = [3,4,5,6], profit = [50,10,40,70] Output: 120 Explanation: The subset chosen is the first and fourth job. Time range [1-3]+[3-6] , we get profit of 120 = 50 + 70.
Example 2:
Input: startTime = [1,2,3,4,6], endTime = [3,5,10,6,9], profit = [20,20,100,70,60] Output: 150 Explanation: The subset chosen is the first, fourth and fifth job. Profit obtained 150 = 20 + 70 + 60.
Example 3:

Input: startTime = [1,1,1], endTime = [2,3,4], profit = [5,6,4] Output: 6
Constraints:
1 <= startTime.length == endTime.length == profit.length <= 5 * 1041 <= startTime[i] < endTime[i] <= 1091 <= profit[i] <= 104When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are offered a massive pile of freelance jobs, and you want to figure out the absolute best combination of jobs to accept so you make the most money without ever doubling up on your schedule. The brute force approach involves looking at every single possible combination of jobs that do not overlap in time, adding up the total payment for each combination, and picking the one that pays the most.
Here's how the algorithm would work step-by-step:
def job_scheduling_brute_force(start_times, end_times, profits):
job_collection = []
for job_index in range(len(start_times)):
job_collection.append((start_times[job_index], end_times[job_index], profits[job_index]))
def explore_job_combinations(current_index, previous_end_time):
# We stop searching when we have evaluated every available job in the collection
if current_index >= len(job_collection):
return 0
current_start, current_end, current_profit = job_collection[current_index]
profit_if_rejected = explore_job_combinations(current_index + 1, previous_end_time)
profit_if_accepted = 0
# We can only accept the current job if it does not overlap with the previous one
if current_start >= previous_end_time:
profit_if_accepted = current_profit + explore_job_combinations(current_index + 1, current_end)
return max(profit_if_rejected, profit_if_accepted)
job_collection.sort(key=lambda job_tuple: job_tuple[0])
# We begin the recursive exploration from the first job with no previous end time
return explore_job_combinations(0, 0)Instead of checking every single combination of jobs, the optimal strategy sorts the jobs by their finish times so we can process them in order. As we consider each job, we cleverly combine its payout with the best possible prior payout from jobs that finished before it could start.
Here's how the algorithm would work step-by-step:
import bisect
def job_scheduling(start_times, end_times, profits):
combined_jobs = sorted(zip(start_times, end_times, profits), key=lambda job: job[1])
timeline_finish = [0]
timeline_profit = [0]
# Sorting by finish times ensures we always consider earlier ending jobs first.
for current_start, current_end, current_profit in combined_jobs:
previous_index = bisect.bisect_right(timeline_finish, current_start) - 1
accumulated_profit = timeline_profit[previous_index] + current_profit
# We take the maximum profit by choosing to either skip or take the current job.
if accumulated_profit > timeline_profit[-1]:
timeline_finish.append(current_end)
timeline_profit.append(accumulated_profit)
# The last entry holds the maximum accumulated profit across all evaluated jobs.
return timeline_profit[-1]| Case | How to Handle |
|---|---|
| Empty input arrays for start, end, or profit | Return zero profit immediately since no jobs exist to schedule. |
| Single job input where arrays contain only one element | Return that single job profit directly without needing any interval comparisons. |
| All jobs overlap with each other in time | The algorithm correctly evaluates each individually and returns the maximum single profit among them. |
| All jobs are completely disjoint and non-overlapping | Dynamic programming with binary search will successfully accumulate and return the sum of all job profits. |
| Jobs with identical start and end times | Sorting by end time ensures zero-duration jobs are processed correctly alongside overlapping intervals. |
| Maximum possible scale with tens of thousands of jobs | Using sorting combined with binary search ensures an efficient O(N log N) time complexity that easily passes within time limits. |
| Extremely large profit values causing integer overflow | Using 64-bit integer types for the profit accumulation prevents arithmetic overflow during summation. |
| Jobs that start at the exact moment another job ends | Treating the condition as non-overlapping allows adjacent back-to-back jobs to be scheduled together. |