Tutorialspoint
Problem
Solution
Submissions

Maximum Profit in Job Scheduling

Certification: Advanced Level Accuracy: 0% Submissions: 0 Points: 20

Write a C program to find the maximum profit that can be obtained by scheduling non-overlapping jobs. Each job has a start time, end time, and profit value. You need to select a subset of jobs such that no two jobs overlap and the total profit is maximized. Two jobs overlap if one starts before the other ends.

Example 1
  • Input: jobs = [[1,3,50],[2,4,10],[3,5,40],[4,6,70]]
  • Output: 120
  • Explanation:
    • We can select jobs [1,3,50] and [4,6,70].
    • These jobs don't overlap (job 1 ends at time 3, job 2 starts at time 4).
    • Total profit = 50 + 70 = 120.
    • This is the maximum possible profit.
Example 2
  • Input: jobs = [[1,2,50],[3,4,10],[2,5,20]]
  • Output: 60
  • Explanation:
    • We can select jobs [1,2,50] and [3,4,10].
    • These jobs don't overlap.
    • Total profit = 50 + 10 = 60. 
Constraints
  • 1 ≤ jobs.length ≤ 50000
  • 1 ≤ startTime[i] < endTime[i] ≤ 10^9
  • 1 ≤ profit[i] ≤ 10^4
  • Time Complexity: O(n log n)
  • Space Complexity: O(n)
Dynamic Programming IBMApple
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Sort jobs by their end times to process them in chronological order
  • Use dynamic programming where dp[i] represents maximum profit using jobs 0 to i
  • For each job, find the latest non-overlapping job using binary search
  • Choose maximum between including current job or excluding it
  • Use memoization to avoid recalculating subproblems

Steps to solve by this approach:

 Step 1: Sort all jobs by their end times to process them chronologically

 Step 2: Create a DP array where dp[i] represents maximum profit using jobs 0 to i
 Step 3: For each job, use binary search to find the latest non-overlapping job
 Step 4: Calculate profit by including current job (its profit + dp[latest non-overlapping])
 Step 5: Calculate profit by excluding current job (dp[i-1])
 Step 6: Take maximum of including and excluding options
 Step 7: Return dp[n-1] as the final maximum profit

Submitted Code :