Maximum Profit in Job Scheduling
HardDynamic Programming1d dp
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
There are n jobs. Job i runs from startTime[i] to endTime[i] and pays profit[i].
Pick a subset of jobs so that no two chosen jobs overlap, and return the maximum total profit. A job that ends at time X does not overlap a job that starts at time X, so both may be chosen.
Aim for O(n log n) time.
Examples
Input: startTime = [1,2,3,3], endTime = [3,4,5,6], profit = [50,10,40,70]
Output: 120
Take the job [1,3] for 50 and the job [3,6] for 70.
Input: startTime = [1,2,3,4,6], endTime = [3,5,10,6,9], profit = [20,20,100,70,60]
Output: 150
Take [1,3] (20), [4,6] (70), and [6,9] (60).
Input: startTime = [1,1,1], endTime = [2,3,4], profit = [5,6,4]
Output: 6
All three jobs overlap at time 1, so only the most profitable one can be taken.
Constraints
- 1 <= startTime.length == endTime.length == profit.length <= 5 * 10^4
- 1 <= startTime[i] < endTime[i] <= 10^9
- 1 <= profit[i] <= 10^4
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.