VOOZH about

URL: https://www.geeksforgeeks.org/dsa/job-sequencing-problem/

⇱ Job Sequencing Problem - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Job Sequencing Problem

Last Updated : 8 Sep, 2025

Given two arrays, deadline[] and profit[], where deadline[i] is the last time unit by which the i-th job must be completed, and profit[i] is the profit earned from completing it.
Each job takes 1 unit time, and only one job can be scheduled at a time. A job earns profit only if finished within its deadline. Find the number of jobs completed and maximum profit.

Examples:

Input: deadline[] = [4, 1, 1, 1], profit[] = [20, 10, 40, 30]
Output: [2, 60]
Explanation: Job 1 (profit 20, deadline 4) can be scheduled. Among the three jobs with deadline 1, only one fits, so we pick the highest profit (40). Hence, 2 jobs with total profit = 60.

Input: deadline[] = [2, 1, 2, 1, 1], profit[] = [100, 19, 27, 25, 15]
Output: [2, 127]
Explanation: Picking the job with profit 100 (deadline 2) and the job with profit 27 (deadline 2); they can occupy the two available slots before deadline 2. Thus 2 jobs are scheduled for a maximum total profit of 127.

[Naive Approach] Using Sorting - O(n2) Time and O(n) Space

The idea is to sort the jobs in descending order of profit and for each job, try to place it in the latest available slot before its deadline. This ensures maximum profit while keeping earlier slots free for other jobs.


Output
2 127

[Expected Approach] Using Sorting and MinHeap - O(n * log(n)) Time and O(n) Space

The idea is to sort the jobs based on their deadlines in ascending order. This ensures that jobs with earlier deadlines are processed first, preventing situations where a job with a short deadline remains unscheduled because a job with a later deadline was chosen instead. We use a min-heap to keep track of the selected jobs, allowing us to efficiently replace lower-profit jobs when a more profitable job becomes available.

Step by Step implementation:

  • Store jobs as pairs of (Deadline, Profit).
  • Sort Jobs array in ascending order of deadline.
  • For each job in the sorted list:
  • If the job can be scheduled within its deadline, push its profit into the heap.
  • If the heap is full (equal to deadline), replace the existing lowest profit job with the current job if it has a higher profit.
  • This ensures that we always keep the most profitable jobs within the available slots.
  • Traverse through the heap and store the total profit and the count of jobs.

Output
2 127

[Alternate Approach] Using Disjoint Set - O(n * log(d)) Time and O(d) Space

The job sequencing can also be done using disjoint set based on the maximum deadline of all the jobs. This approach is explained in article Job Sequencing - Using Disjoint Set.

Comment
Article Tags: