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 from non-overlapping job schedules, given start and end times and corresponding profits.

Example 1
  • Input: startTime = [1,2,3,3] endTime = [3,4,5,6] profit = [50,10,40,70]
  • Output: 120
  • Explanation:
    • Jobs at index 0 and 3 yield maximum profit without overlap.
    • Job 0: Start at time 1, end at time 3, profit = 50
    • Job 3: Start at time 3, end at time 6, profit = 70
    • Total profit = 50 + 70 = 120
Example 2
  • Input: startTime = [1,2,3,4,6] endTime = [3,5,10,6,9] profit = [20,20,100,70,60]
  • Output: 150
  • Explanation:
    • Jobs at index 0, 3, and 4 yield maximum profit:
    • Job 0: Start at time 1, end at time 3, profit = 20
    • Job 3: Start at time 4, end at time 6, profit = 70
    • Job 4: Start at time 6, end at time 9, profit = 60
    • Total profit = 20 + 70 + 60 = 150
Constraints
  • 1 ≤ startTime.length ≤ 5×10⁴
  • startTime[i] < endTime[i] ≤ 10⁹
  • 1 ≤ profit[i] ≤ 10⁴
  • All jobs have distinct start times
  • Time Complexity: O(n log n)
  • Space Complexity: O(n)
Dynamic Programming AlgorithmsCapgeminiSnowflake
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 start time to process them in chronological order
  • Use dynamic programming to keep track of the maximum profit up to each job
  • Implement binary search to find the latest non-overlapping job
  • Consider using memoization to avoid redundant calculations
  • The solution requires combining sorting, binary search, and dynamic programming techniques

Steps to solve by this approach:

 Step 1: Combine start, end, and profit into a single structure
 Step 2: Sort jobs by end time
 Step 3: Use DP to store max profit up to each job
 Step 4: For each job, find previous non-overlapping job using binary search
 Step 5: Compute profit including current job and compare with excluding it
 Step 6: Return the maximum value in DP table

Submitted Code :