AlgoMaster Logo

Maximum Profit in Job Scheduling

Ashish

Ashish Pratap Singh

hard

Problem Description

Solve it on LeetCode

Approaches

1. Recursion with Memoization (Dynamic Programming)

Intuition:

The problem can be broken down into deciding which jobs to take to maximize the profit. For each job, you have a choice of either taking the job or skipping it. If you take the job, you cannot take any other jobs overlapping with it. Dynamic programming with memoization helps avoid recalculating results of overlapping subproblems.

Steps:

  1. Sort Jobs by End Time: This will allow us to process jobs in a logical order (left to right in scheduling terms).
  2. Use Dynamic Programming with Recursion:
    • Define a recursive function maxProfit(index) that returns the maximum profit starting from the job at index.
    • Check two cases:
      1. Skip the current job and call maxProfit(index + 1).
      2. Include the current job, find the next job that can start after the current job ends using binary search, and call maxProfit(nextIndex).
    • Use memoization to store and reuse results for each index.

Code:

Intuition:

Instead of using recursion, this approach uses an iterative method to build up solutions for every job from left to right. The addition of binary search helps in quickly finding the next available job that doesn't overlap with the current job.

Steps:

  1. Sort Jobs by End Time: Same as before, sort jobs to handle them in scheduling order.
  2. Dynamic Array dpdp[i] holds the maximum profit considering jobs up to i.
  3. Iterate Over Each Job:
    • Use binary search to find the maximum profit of the non-overlapping job.
    • Update dp[i] by comparing the addition of the current job's profit and the maximum profit of the last non-overlapping job with dp[i-1].

Code: