Limited Time Offer:Up to 0% off Hello Interview Premium
Up to 0% off Hello Interview Premium 🎉
Hello Interview
Learn Code
Introduction
Overview
Container With Most Water
Two Sum (Sorted Array)
3-Sum
Triangle Numbers
Move Zeroes
Sort Colors
Trapping Rain Water
Overview
Maximum Sum of Subarrays of Size K
Max Points You Can Obtain From Cards
Max Sum of Distinct Subarrays Length k
Overview
Longest Substring Without Repeating Characters
Longest Repeating Character Replacement
Overview
Can Attend Meetings
Insert Interval
Non-Overlapping Intervals
Merge Intervals
Employee Free Time
Overview
Valid Parentheses
Decode String
Longest Valid Parentheses
Monotonic Stack
Daily Temperatures
Largest Rectangle in Histogram
Overview
Linked List Cycle
Palindrome Linked List
Remove Nth Node From End of List
Reorder List
Swap Nodes in Pairs
Overview
Apple Harvest (Koko Eating Bananas)
Search in Rotated Sorted Array
Split Array Largest Sum
Kth Smallest Element in a Sorted Matrix
Minimum Shipping Capacity
Overview
Kth Largest Element in an Array
K Closest Points to Origin
Find K Closest Elements
Merge K Sorted Lists
Median from Data Stream
Introduction
Fundamentals
Return Values
Maximum Depth of Binary Tree
Path Sum
Passing Values Down and Helper Functions
Validate Binary Search Tree
Calculate Tilt
Diameter of a Binary Tree
Path Sum II
Longest Univalue Path
Graphs Overview
Adjacency List
Copy Graph
Graph Valid Tree
Matrices
Flood Fill
Number of Islands
Surrounded Regions
Pacific Atlantic Water Flow
Introduction
Overview
Level Order Sum
Rightmost Node
Zigzag Level Order
Maximum Width of Binary Tree
Graphs Overview
Minimum Knight Moves
Rotting Oranges
01-Matrix
Bus Routes
Overview
Word Search
Solution Space Trees
Subsets
Generate Parentheses
Combination Sum
Palindrome Partitioning
N-Queens
Overview
Course Schedule
Course Schedule II
Shortest Path Algorithms
Network Delay Time
Cheapest Flights Within K Stops
Path With Minimum Effort
Find City with Fewest Reachable
Fundamentals
Solving a Question with Dynamic Programming
Counting Bits
Decode Ways
Unique Paths
Maximal Square
Longest Increasing Subsequence
Word Break
Maximum Profit in Job Scheduling
Paint House
Paint House II
Minimum Window Subsequence
Overview
Best Time to Buy and Sell Stock
Gas Station
Jump Game
Jump Game II
Partition Labels
Overview
Implement Trie Methods
Prefix Matching
Overview
Count Vowels in Substrings
Subarray Sum Equals K
Spiral Matrix
Rotate Image
Set Matrix Zeroes
Vote For New Content
Pricing
Sign in / Sign up
Search
⌘K
Pricing

Tutor

Dynamic Programming

Maximum Profit in Job Scheduling

medium

max (21)341224132;341225102;21365487109
Count: 10
abcValid triangle requires:a + b > c AND a + c > b AND b + c > a(every pair must sum to more than the third side)3511SOURCE23211SOURCE23UNREACHABLE$100$100$100$5000SRC123DST$100$100$1000SRC123DST01233141Threshold: 4Answer: 32 reachable01234231118Threshold: 2Answer: 01 reachable1102233321432263321
DESCRIPTION (inspired by Leetcode.com)

Given three arrays, starts, ends, and profits, each representing the start time, end time, and profit of jobs respectively, your task is to schedule the jobs to maximize total profit. You can only work on one job at a time, and once a job has been started, you must finish it before starting another. The goal is to find the maximum profit that can be earned by scheduling jobs such that they do not overlap.

Input:

starts = [1, 3, 6, 10]
ends = [4, 5, 10, 12]
profits = [20, 20, 100, 70]

The optimal solution would schedule the jobs as follows:

Start the first job at time 1 and complete it by 4 for a profit of 20. Start the next job at time 6 and complete it by 10 for a profit of 100. Start the last job at time 10 and complete it by 12 for a profit of 70.

The total profit is 20 + 100 + 70 = 190

Explanation

Note: The solution below uses the key parameter in the bisect_right function to find the latest job ending before the start time of the current job, which is only available in Python 3.10 and above.
The idea behind this solution is to first sort the jobs by their end times. Afterwards, we initialize an array dp of length len(jobs) + 1 with 0s, where dp[i] represents the maximum profit that can be earned by scheduling the first i jobs (sorted by end time). dp[0] is initialized to 0, as scheduling 0 jobs would yield 0 profit.
Visualization
Python
Language
from bisect import bisect_right
def job_scheduling(starts, ends, profits):
jobs = sorted(zip(starts, ends, profits),
key=lambda x: x[1])
dp = [0] * (len(jobs) + 1)
for i in range(1, len(jobs) + 1):
start, end, profit = jobs[i - 1]
# find number of jobs to finish before start of current job
num_jobs = bisect_right([job[1] for job in jobs], start)
dp[i] = max(dp[i - 1], dp[num_jobs] + profit)
return dp[-1]

maximum profit in job scheduling

0 / 2

Sorting and initializing `dp` array
Next, we iterate over each index in dp starting from index 1. At each iteration, we calculate the maximum profit that can be earned by scheduling the first i jobs (sorted by end time). So when i = 1, we are calculating the maximum profit that can be earned by scheduling the first job.
1). We find the corresponding start time and profit of the current job (jobs[i - 1]). 2). Next, we use binary search (bisect_right) to find the number of jobs that have ended before the start time of the current job, and store this number in a variable num_jobs. dp[num_jobs] will also tell us the maximum profit that can be earned by scheduling all the jobs that have ended before the current job.
To breakdown our call to bisect_right: we are looking for the rightmost index in job in job with an end time (key=lambda x: x[1]) that is less than or equal to the start time of the current job (start).
In this case, start = 1, which means this is the first job we can possibly schedule, and num_jobs = 0.
Visualization
Python
Language
from bisect import bisect_right
def job_scheduling(starts, ends, profits):
jobs = sorted(zip(starts, ends, profits),
key=lambda x: x[1])
dp = [0] * (len(jobs) + 1)
for i in range(1, len(jobs) + 1):
start, end, profit = jobs[i - 1]
# find number of jobs to finish before start of current job
num_jobs = bisect_right([job[1] for job in jobs], start)
dp[i] = max(dp[i - 1], dp[num_jobs] + profit)
return dp[-1]
][4120][5318][8470][10510000000

initialize dp array

0 / 2

Finding the latest job ending before the start time of the current job
Once we have this index, we have two options:
  1. We can schedule the current job, in which case the profit would be dp[num_jobs] + profit.
  2. We can skip the current job, in which case the profit would be dp[i - 1].
By taking the maximum of these two options, we can calculate the maximum profit that can be earned by scheduling the first i jobs.
In this case, since this is the first job we can possibly schedule, we schedule it and update dp[i] to dp[num_jobs] + profit.
Visualization
Python
Language
from bisect import bisect_right
def job_scheduling(starts, ends, profits):
jobs = sorted(zip(starts, ends, profits),
key=lambda x: x[1])
dp = [0] * (len(jobs) + 1)
for i in range(1, len(jobs) + 1):
start, end, profit = jobs[i - 1]
# find number of jobs to finish before start of current job
num_jobs = bisect_right([job[1] for job in jobs], start)
dp[i] = max(dp[i - 1], dp[num_jobs] + profit)
return dp[-1]
][4120][5318][8470][10510000000num_jobs = 0i = 1

num_jobs = 0

0 / 1

Calculating the maximum profit that can be earned by scheduling the first job

Skipping the current job

The next iteration is an example of when we would choose to skip the current job. For this job, start = 3. This job overlaps with the first job, so num_jobs = 0. If we consider the two cases:
  1. We can schedule the current job, in which case the profit would be dp[num_jobs] + profit = 0 + 18 = 18.
  2. We can skip the current job, in which case the profit would be dp[i - 1] = 20.
We can see that skipping the current job (in favor of choosing the previous one) would yield a higher profit, so we update dp[i] to dp[i - 1].
Visualization
Python
Language
from bisect import bisect_right
def job_scheduling(starts, ends, profits):
jobs = sorted(zip(starts, ends, profits),
key=lambda x: x[1])
dp = [0] * (len(jobs) + 1)
for i in range(1, len(jobs) + 1):
start, end, profit = jobs[i - 1]
# find number of jobs to finish before start of current job
num_jobs = bisect_right([job[1] for job in jobs], start)
dp[i] = max(dp[i - 1], dp[num_jobs] + profit)
return dp[-1]
][4120][5318][8470][105100020000num_jobs = 0i = 1

dp[1] = max(0, 0 + 20)

0 / 3

Calculating the maximum profit that can be earned by scheduling the first job
If we examine the state of the dp array at this point, it tells us at that the maximum profit we can obtain from scheduling any combination of the first two jobs is 20.
We can continue this process for the remaining jobs, and the final value of dp[-1] will tell us the maximum profit that can be earned by scheduling all the jobs.

Solution

Visualization
Python
Language
Try these examples:
from bisect import bisect_right
def job_scheduling(starts, ends, profits):
jobs = sorted(zip(starts, ends, profits),
key=lambda x: x[1])
dp = [0] * (len(jobs) + 1)
for i in range(1, len(jobs) + 1):
start, end, profit = jobs[i - 1]
# find number of jobs to finish before start of current job
num_jobs = bisect_right([job[1] for job in jobs], start)
dp[i] = max(dp[i - 1], dp[num_jobs] + profit)
return dp[-1]

maximum profit in job scheduling

0 / 15

Test Your Knowledge

Login to take the complexity quiz and track your progress

Complexity Analysis

Time Complexity: O(n * log n) where n is the number of jobs. This is due to the sorting of the jobs, and the binary search for each job.

Space Complexity: O(n) where n is the number of jobs. This is due to the dp array.

Mark as read

Next: Paint House

Your account is free and you can post anonymously if you choose.

Unlock Premium Coding Content

Interactive algorithm visualizations
Guided Practice
Recent interview questions
Learn More
Reading Progress

On This Page

Explanation

Solution

Questions
Meta SWE Interview QuestionsAmazon SWE Interview QuestionsGoogle SWE Interview QuestionsOpenAI SWE Interview QuestionsAnthropic SWE Interview QuestionsEngineering Manager (EM) Interview Questions
Learn
Learn System DesignLearn DSALearn BehavioralLearn ML System DesignLearn Low Level DesignGuided Practice
Links
FAQPricingGift PremiumHello Interview Premium
Legal
Terms and ConditionsPrivacy PolicySecurity
Contact
About UsProduct Support

7511 Greenwood Ave North Unit #4238 Seattle WA 98103


© 2026 Optick Labs Inc. All rights reserved.

Login to track your progress