Tutorialspoint
Problem
Solution
Submissions

Maximum Profit in Job Scheduling

Certification: Advanced Level Accuracy: 100% Submissions: 1 Points: 15

Write a Java program to find the maximum profit you can achieve by scheduling jobs. You have n jobs, where every job is scheduled to be done from startTime[i] to endTime[i], obtaining a profit of profit[i]. You need to find the maximum profit you can get if you can only work on one job at a time.

Example 1
  • Input: startTime = [1, 2, 3, 3] endTime = [3, 4, 5, 6] profit = [50, 10, 40, 70]
  • Output: 120
  • Explanation:
     The jobs are:
     - Job 1: Start at time 1, end at time 3, profit 50
     - Job 2: Start at time 2, end at time 4, profit 10
     - Job 3: Start at time 3, end at time 5, profit 40
     - Job 4: Start at time 3, end at time 6, profit 70
    The maximum profit can be achieved by scheduling Job 1 and Job 4. Job 1 starts at time 1 and ends at time 3, with profit 50. Job 4 starts at time 3 and ends at time 6, with 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:
     The jobs are:
     - Job 1: Start at time 1, end at time 3, profit 20
     - Job 2: Start at time 2, end at time 5, profit 20
     - Job 3: Start at time 3, end at time 10, profit 100
     - Job 4: Start at time 4, end at time 6, profit 70
     - Job 5: Start at time 6, end at time 9, profit 60
    The maximum profit can be achieved by scheduling Job 1 and Job 4 and Job 5. Job 1 starts at time 1 and ends at time 3, with profit 20. Job 4 starts at time 4 and ends at time 6, with profit 70. Job 5 starts at time 6 and ends at time 9, with profit 60. Total profit = 20 + 70 + 60 = 150.
Constraints
  • 1 ≤ startTime.length == endTime.length == profit.length ≤ 5 * 10^4
  • 1 ≤ startTime[i] < endTime[i] ≤ 10^9
  • 1 ≤ profit[i] ≤ 10^4
  • Time Complexity: O(n log n)
  • Space Complexity: O(n)
ArraysDynamic Programming WiproDeloitte
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 the jobs based on their end times
  • Use dynamic programming to find the maximum profit
  • For each job, you have two choices: either take the job or don't take it
  • If you take a job, you can't take jobs that overlap with it
  • Use binary search to find the latest job that finishes before the current job starts
  • Build a bottom-up DP array where dp[i] represents the maximum profit up to the ith job
  • For each job, update dp[i] = max(profit[i] + dp[j], dp[i-1]), where j is the latest non-overlapping job

Steps to solve by this approach:

 Step 1: Create a jobs array where each job is represented as [startTime, endTime, profit].

 Step 2: Sort all jobs by their end times to process them in order of completion.
 Step 3: Initialize a dynamic programming array dp, where dp[i] represents the maximum profit up to the ith job.
 Step 4: For each job, we have two choices: either take the job or skip it.
 Step 5: If we take the job, we need to find the latest job that doesn't overlap with the current job and add its profit.
 Step 6: If we skip the job, the maximum profit is the same as the previous job's maximum profit.
 Step 7: For each job, dp[i] = max(profit[i] + dp[j], dp[i-1]), where j is the latest non-overlapping job.

Submitted Code :