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.

Try It Yourself
redirect icon

[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.

C++
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

vector<int> jobSequencing(vector<int> &deadline,
                           vector<int> &profit) {
    int n = deadline.size();
    int cnt = 0;
    int totProfit = 0;

    // pair the profit and deadline of
    // all the jos together
    vector<pair<int, int>> jobs;
    for (int i = 0; i < n; i++) {
        jobs.push_back({profit[i], deadline[i]});
    }

    // sort the jobs based on profit
    // in decreasing order
    sort(jobs.begin(), jobs.end(), 
                greater<pair<int, int>>());

    vector<int> slot(n, 0);
    for (int i = 0; i < n; i++) {
        int start = min(n, jobs[i].second) - 1;
        for (int j = start; j >= 0; j--) {

            // if slot is empty
            if (slot[j] == 0) {
                slot[j] = 1;
                cnt++;
                totProfit+= jobs[i].first;
                break;
            }
        }
    }
    
    return {cnt, totProfit};
}

int main() {
    vector<int> deadline = {2, 1, 2, 1, 1};
    vector<int> profit = {100, 19, 27, 25, 15};
    vector<int> ans = jobSequencing(deadline, profit);
    cout<<ans[0]<<" "<<ans[1];
    return 0;
}
Java
import java.util.ArrayList;

class GfG {
     public static ArrayList<Integer> jobSequencing(int[] deadline, int[] profit) {
        int n = deadline.length;
        int cnt = 0;
        int totProfit = 0;

        // pair the profit and deadline of all the jobs together
        ArrayList<int[]> jobs = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            jobs.add(new int[]{profit[i], deadline[i]});
        }

        // sort the jobs based on profit in decreasing order
        jobs.sort((a, b) -> Integer.compare(b[0], a[0]));
        int[] slot = new int[n];

        for (int i = 0; i < n; i++) {
            int start = Math.min(n, jobs.get(i)[1]) - 1;
            for (int j = start; j >= 0; j--) {

                // if slot is empty
                if (slot[j] == 0) {
                    slot[j] = 1;
                    cnt++;
                    totProfit += jobs.get(i)[0];
                    break;
                }
            }
        }

        ArrayList<Integer> result = new ArrayList<>();
        result.add(cnt);
        result.add(totProfit);
        return result;
    }

    public static void main(String[] args) {
        int[] deadline = {2, 1, 2, 1, 1};
        int[] profit = {100, 19, 27, 25, 15};

        ArrayList<Integer> ans = jobSequencing(deadline, profit);
        System.out.println(ans.get(0) + " " + ans.get(1));
    }
}
Python
from typing import List, Tuple


def jobSequencing(deadline: List[int], profit: List[int]) -> List[int]:
    n = len(deadline)
    cnt = 0
    totProfit = 0

    # pair the profit and deadline of
    # all the jobs together
    jobs = [(profit[i], deadline[i]) for i in range(n)]

    # sort the jobs based on profit
    # in decreasing order
    jobs.sort(key=lambda x: x[0], reverse=True)

    slot = [0] * n
    for i in range(n):
        start = min(n, jobs[i][1]) - 1
        for j in range(start, -1, -1):

            # if slot is empty
            if slot[j] == 0:
                slot[j] = 1
                cnt += 1
                totProfit += jobs[i][0]
                break

    return [cnt, totProfit]


if __name__ == "__main__":
    deadline = [2, 1, 2, 1, 1]
    profit = [100, 19, 27, 25, 15]
    ans = jobSequencing(deadline, profit)
    print(ans[0], ans[1])
C#
using System;
using System.Collections.Generic;

class GfG {
    static List<int> jobSequencing(int[] deadline, int[] profit) {
        int n = deadline.Length;
        int cnt = 0;
        int totProfit = 0;
        List<Tuple<int, int>> jobs = new List<Tuple<int, int>>();
        
        // pair the profit and deadline of
        // all the jos together
        for (int i = 0; i < n; i++) {
            jobs.Add(new Tuple<int, int>(profit[i], deadline[i]));
        }

        // sort the jobs based on profit
        // in decreasing order
        jobs.Sort((a, b) => b.Item1.CompareTo(a.Item1));
        
        int[] slot = new int[n];
        
        for (int i = 0; i < n; i++) {
            int start = Math.Min(n, jobs[i].Item2) - 1;
            for (int j = start; j >= 0; j--) {
            
                // if slot is empty
                if (slot[j] == 0) {
                    slot[j] = 1;
                    cnt++;
                    totProfit += jobs[i].Item1;
                    break;
                }
            }
        }
        
        return new List<int> { cnt, totProfit };
    }

    static void Main() {
        int[] deadline = { 2, 1, 2, 1, 1 };
        int[] profit = { 100, 19, 27, 25, 15 };
        List<int> ans = jobSequencing(deadline, profit);
        Console.WriteLine(ans[0] + " " + ans[1]);
    }
}
JavaScript
function jobSequencing(deadline, profit) {
    let n = deadline.length;
    let cnt = 0;
    let totProfit = 0;

    // pair the profit and deadline of
    // all the jobs together
    let jobs = [];
    for (let i = 0; i < n; i++) {
        jobs.push([profit[i], deadline[i]]);
    }

    // sort the jobs based on profit
    // in decreasing order
    jobs.sort((a, b) => b[0] - a[0]);

    let slot = Array(n).fill(0);
    for (let i = 0; i < n; i++) {
        let start = Math.min(n, jobs[i][1]) - 1;
        for (let j = start; j >= 0; j--) {

            // if slot is empty
            if (slot[j] === 0) {
                slot[j] = 1;
                cnt++;
                totProfit += jobs[i][0];
                break