Open In App

Weighted Job Scheduling

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a 2D array jobs[][] of order n*3, where each element jobs[i] defines start time, end time, and the profit associated with the job. The task is to find the maximum profit you can take such that there are no two jobs with overlapping time ranges.

Note: If the job ends at time X, it is allowed to choose another job that starts at time X.

Examples: 

Input: jobs[][] = [[1, 2, 50],
[3, 5, 20],
[6, 19, 100],
[2, 100, 200]]
Output: 250
Explanation: The first and fourth jobs with the time range [1, 2] and [2, 100] can be chosen to give maximum profit of 50 + 200 = 250.

Input: jobs[][] = [[1, 3, 60],
[2, 5, 50],
[4, 6, 70],
[5, 7, 30]]
Output: 130
Explanation: The first and third jobs with the time range [1, 3] and [4, 6] can be chosen to give maximum profit of 60 + 70 = 130.

[Naive Approach] - Using Recursion - O(n ^ n) Time and O(n) Space

The idea is to schedule the jobs in all possible ways using recursion and find the profit associated with each scheduling, the maximum among them is the result. To do so, sort the jobs in ascending order based on their start time and for each job, consider the job's profit, and find the maximum profit from all the next non-overlapping jobs i.e. maxProfit(i, jobs) = max(maxProfit(j, jobs)), where i < j < n.

C++
// C++ program to implement 
// Weighted Job Scheduling 
#include <bits/stdc++.h>
using namespace std;

// Recusive function to find the maximum 
// profit from weighted job scheduling
int maxProfitRecur(vector<vector<int>> &jobs, 
                            int ind, int last) {
    if (ind == jobs.size())
        return 0;

    // skip the current job
    int ans = maxProfitRecur(jobs, ind+1, last);
    
    // if start of current is greater than or
    // equals to end time of previous job
    if(jobs[ind][0] >= last)
        ans = max(ans, jobs[ind][2] + 
        maxProfitRecur(jobs, ind+1, jobs[ind][1]));

    return ans;
}

// Function to find the maximum profit
// from weighted job scheduling
int maxProfit(vector<vector<int>> &jobs) {
    int n = jobs.size();

    // Sort the jobs based on start time
    sort(jobs.begin(), jobs.end());

    return maxProfitRecur(jobs, 0, -1);
}

int main() {
    vector<vector<int>> jobs = {
        {1, 2, 50}, 
        {3, 5, 20}, 
        {6, 19, 100}, 
        {2, 100, 200}
    };
    cout << maxProfit(jobs);
    return 0;
}
Java
// Java program to implement 
// Weighted Job Scheduling
import java.util.*;

class GfG {
  
// Recursive function to find the maximum 
// profit from weighted job scheduling
static int maxProfitRecur(int[][] jobs, 
                        int ind, int last) {
    if (ind == jobs.length)
        return 0;

    // skip the current job
    int ans = maxProfitRecur(jobs, ind + 1, last);

    // if start of current is greater than or
    // equals to end time of previous job
    if (jobs[ind][0] >= last)
        ans = Math.max(ans, jobs[ind][2] +
        maxProfitRecur(jobs, ind + 1, jobs[ind][1]));

    return ans;
}

// Function to find the maximum profit
// from weighted job scheduling
static int maxProfit(int[][] jobs) {

    // Sort the jobs based on start time
    Arrays.sort(jobs, (a, b) -> 
            Integer.compare(a[0], b[0]));

    return maxProfitRecur(jobs, 0, -1);
}

public static void main(String[] args) {
    int[][] jobs = {
        {1, 2, 50},
        {3, 5, 20},
        {6, 19, 100},
        {2, 100, 200}
    };
    System.out.println(maxProfit(jobs));
}
}
Python
# Python program to implement 
# Weighted Job Scheduling

# Recursive function to find the maximum 
# profit from weighted job scheduling
def maxProfitRecur(jobs, ind, last):
    if ind == len(jobs):
        return 0

    # skip the current job
    ans = maxProfitRecur(jobs, ind + 1, last)

    # if start of current is greater than or
    # equals to end time of previous job
    if jobs[ind][0] >= last:
        ans = max(ans, jobs[ind][2] +
              maxProfitRecur(jobs, ind + 1, jobs[ind][1]))

    return ans

# Function to find the maximum profit
# from weighted job scheduling
def maxProfit(jobs):
  
    # Sort the jobs based on start time
    jobs.sort()

    return maxProfitRecur(jobs, 0, -1)

if __name__ == "__main__":
    jobs = [
        [1, 2, 50],
        [3, 5, 20],
        [6, 19, 100],
        [2, 100, 200]
    ]
    print(maxProfit(jobs))
C#
// C# program to implement 
// Weighted Job Scheduling
using System;
using System.Collections.Generic;

class GfG {

// Recursive function to find the maximum 
// profit from weighted job scheduling
static int MaxProfitRecur(int[][] jobs, 
                    int ind, int last) {
    if (ind == jobs.Length)
        return 0;

    // skip the current job
    int ans = MaxProfitRecur(jobs, ind + 1, last);

    // if start of current is greater than or
    // equals to end time of previous job
    if (jobs[ind][0] >= last)
        ans = Math.Max(ans, jobs[ind][2] + 
        MaxProfitRecur(jobs, ind + 1, jobs[ind][1]));

    return ans;
}

// Function to find the maximum profit
// from weighted job scheduling
static int MaxProfit(int[][] jobs) {

    // Sort the jobs based on start time
    Array.Sort(jobs, (a, b) => a[0].CompareTo(b[0]));

    return MaxProfitRecur(jobs, 0, -1);
}

static void Main(string[] args) {
    int[][] jobs = {
        new int[] {1, 2, 50},
        new int[] {3, 5, 20},
        new int[] {6, 19, 100},
        new int[] {2, 100, 200}
    };
    Console.WriteLine(MaxProfit(jobs));
}
}
JavaScript
// JavaScript program to implement 
// Weighted Job Scheduling

// Recursive function to find the maximum 
// profit from weighted job scheduling
function maxProfitRecur(jobs, ind, last) {
    if (ind === jobs.length)
        return 0;

    // skip the current job
    let ans = maxProfitRecur(jobs, ind + 1, last);

    // if start of current is greater than or
    // equals to end time of previous job
    if (jobs[ind][0] >= last)
        ans = Math.max(ans, jobs[ind][2] + 
        maxProfitRecur(jobs, ind + 1, jobs[ind][1]));

    return ans;
}

// Function to find the maximum profit
// from weighted job scheduling
function maxProfit(jobs) {

    // Sort the jobs based on start time
    jobs.sort((a, b) => a[0] - b[0]);

    return maxProfitRecur(jobs, 0, -1);
}

// Driver Code
const jobs = [
    [1, 2, 50],
    [3, 5, 20],
    [6, 19, 100],
    [2, 100, 200]
];
console.log(maxProfit(jobs));

Output
250

[Expected Approach - 1] - Using Memoization - O(n ^ 2) Time and O(n) Space

The above approach can be optimized using memoization to avoid computing the overlapping subproblems multiple times. To do so, create an array memo[] of size n, where each element memo[i] stores the maximum possible profit for jobs in range [i, n-1]. For each recursive call, check if the subproblem is already computed, if so return the stored value, else proceed similar to above approach.

C++
// C++ program to implement 
// Weighted Job Scheduling 
#include <bits/stdc++.h>
using namespace std;

// Recusive function to find the maximum 
// profit from weighted job scheduling
int maxProfitRecur(vector<vector<int>> &jobs, 
        int ind, int last, vector<int> &memo) {
    if (ind == jobs.size())
        return 0;

    // if the job can be taken
    if(jobs[ind][0] >= last) {

        // if the value is not calculated
        if(memo[ind] == -1) {

            // take the job
            memo[ind] = jobs[ind][2] + 
            maxProfitRecur(jobs, ind+1, jobs[ind][1], memo);

            // leave the job and find max
            memo[ind] = max(memo[ind], 
                maxProfitRecur(jobs, ind+1, last, memo));
        }
            
        return memo[ind];
    }
    
    // if the job can't be taken
    return maxProfitRecur(jobs, ind+1, last, memo);
}

// Function to find the maximum profit
// from weighted job scheduling
int maxProfit(vector<vector<int>> &jobs) {
    int n = jobs.size();

    // Sort the jobs based on start time
    sort(jobs.begin(), jobs.end());

    // create memoization array and 
    // initialize it with -1
    vector<int> memo(n, -1);

    return maxProfitRecur(jobs, 0, -1, memo);
}

int main() {
    vector<vector<int>> jobs = {
        {1, 2, 50}, 
        {3, 5, 20}, 
        {6, 19, 100}, 
        {2, 100, 200}
    };
    cout << maxProfit(jobs);
    return 0;
}
Java
// Java program to implement 
// Weighted Job Scheduling
import java.util.*;

class GfG {

// Recursive function to find the maximum 
// profit from weighted job scheduling
static int maxProfitRecur(int[][] jobs, 
            int ind, int last, int[] memo) {
    if (ind == jobs.length)
        return 0;

    // if the job can be taken
    if (jobs[ind][0] >= last) {

        // if the value is not calculated
        if (memo[ind] == -1) {

            // take the job
            memo[ind] = jobs[ind][2] +
            maxProfitRecur(jobs, ind + 1, jobs[ind][1], memo);

            // leave the job and find max
            memo[ind] = Math.max(memo[ind],
            maxProfitRecur(jobs, ind + 1, last, memo));
        }

        return memo[ind];
    }

    // if the job can't be taken
    return maxProfitRecur(jobs, ind + 1, last, memo);
}

// Function to find the maximum profit
// from weighted job scheduling
static int maxProfit(int[][] jobs) {
    int n = jobs.length;

    // Sort the jobs based on start time
    Arrays.sort(jobs, (a, b) -> 
            Integer.compare(a[0], b[0]));

    // create memoization array and 
    // initialize it with -1
    int[] memo = new int[n];
    Arrays.fill(memo, -1);

    return maxProfitRecur(jobs, 0, -1, memo);
}

public static void main(String[] args) {
    int[][] jobs = {
        {1, 2, 50},
        {3, 5, 20},
        {6, 19, 100},
        {2, 100, 200}
    };
    System.out.println(maxProfit(jobs));
}
}
Python
# Python program to implement 
# Weighted Job Scheduling

# Recursive function to find the maximum 
# profit from weighted job scheduling
def maxProfitRecur(jobs, ind, last, memo):
    if ind == len(jobs):
        return 0

    # if the job can be taken
    if jobs[ind][0] >= last:

        # if the value is not calculated
        if memo[ind] == -1:

            # take the job
            memo[ind] = jobs[ind][2] + \
                maxProfitRecur(jobs, ind + 1, jobs[ind][1], memo)

            # leave the job and find max
            memo[ind] = max(memo[ind],
                      maxProfitRecur(jobs, ind + 1, last, memo))

        return memo[ind]

    # if the job can't be taken
    return maxProfitRecur(jobs, ind + 1, last, memo)

# Function to find the maximum profit
# from weighted job scheduling
def maxProfit(jobs):
  
    # Sort the jobs based on start time
    jobs.sort()

    # create memoization array and 
    # initialize it with -1
    memo = [-1] * len(jobs)

    return maxProfitRecur(jobs, 0, -1, memo)

if __name__ == "__main__":
    jobs = [
        [1, 2, 50],
        [3, 5, 20],
        [6, 19, 100],
        [2, 100, 200]
    ]
    print(maxProfit(jobs))
C#
// C# program to implement 
// Weighted Job Scheduling
using System;
using System.Collections.Generic;

class GfG {

// Recursive function to find the maximum 
// profit from weighted job scheduling
static int MaxProfitRecur(int[][] jobs,
        int ind, int last, int[] memo) {
    if (ind == jobs.Length)
        return 0;

    // if the job can be taken
    if (jobs[ind][0] >= last) {

        // if the value is not calculated
        if (memo[ind] == -1) {

            // take the job
            memo[ind] = jobs[ind][2] +
            MaxProfitRecur(jobs, ind + 1, jobs[ind][1], memo);

            // leave the job and find max
            memo[ind] = Math.Max(memo[ind],
            MaxProfitRecur(jobs, ind + 1, last, memo));
        }

        return memo[ind];
    }

    // if the job can't be taken
    return MaxProfitRecur(jobs, ind + 1, last, memo);
}

// Function to find the maximum profit
// from weighted job scheduling
static int MaxProfit(int[][] jobs) {
    int n = jobs.Length;

    // Sort the jobs based on start time
    Array.Sort(jobs, (a, b) => a[0].CompareTo(b[0]));

    // create memoization array and 
    // initialize it with -1
    int[] memo = new int[n];
    Array.Fill(memo, -1);

    return MaxProfitRecur(jobs, 0, -1, memo);
}

static void Main(string[] args) {
    int[][] jobs = {
        new int[] {1, 2, 50},
        new int[] {3, 5, 20},
        new int[] {6, 19, 100},
        new int[] {2, 100, 200}
    };
    Console.WriteLine(MaxProfit(jobs));
}
}
JavaScript
// JavaScript program to implement 
// Weighted Job Scheduling

// Recursive function to find the maximum 
// profit from weighted job scheduling
function maxProfitRecur(jobs, ind, last, memo) {
    if (ind === jobs.length)
        return 0;

    // if the job can be taken
    if (jobs[ind][0] >= last) {

        // if the value is not calculated
        if (memo[ind] === -1) {

            // take the job
            memo[ind] = jobs[ind][2] + 
            maxProfitRecur(jobs, ind + 1, jobs[ind][1], memo);

            // leave the job and find max
            memo[ind] = Math.max(memo[ind], 
            maxProfitRecur(jobs, ind + 1, last, memo));
        }

        return memo[ind];
    }

    // if the job can't be taken
    return maxProfitRecur(jobs, ind + 1, last, memo);
}

// Function to find the maximum profit
// from weighted job scheduling
function maxProfit(jobs) {
    // Sort the jobs based on start time
    jobs.sort((a, b) => a[0] - b[0]);

    // create memoization array and 
    // initialize it with -1
    const memo = Array(jobs.length).fill(-1);

    return maxProfitRecur(jobs, 0, -1, memo);
}

const jobs = [
    [1, 2, 50],
    [3, 5, 20],
    [6, 19, 100],
    [2, 100, 200]
];
console.log(maxProfit(jobs));

Output
250

[Expected Approach - 2] - Using Tabulation - O(n * n) Time and O(n) Space

The above approach can further be optimized using bottom-up dp (tabulation) to minimize the space required for recursive stack. To do so, create an array dp[] of size n, where each element dp[i] stores the maximum possible profit for jobs in range [i, n-1]. Start from the last index i.e. n-1, and for each index i compute the maximum profit for subarray i to n-1 and store the result in dp[i]. The maximum of all the values stored in dp[] is the result.

C++
// C++ program to implement 
// Weighted Job Scheduling 
#include <bits/stdc++.h>
using namespace std;

// Function to find the maximum profit
// from weighted job scheduling
int maxProfit(vector<vector<int>> &jobs) {
    int n = jobs.size();

    // Sort the jobs based on start time
    sort(jobs.begin(), jobs.end());

    // create a dp table
    vector<int> dp(n);

    // stores the maximum profit
    int res = 0;
    
    // iterate over all the jobs
    for(int i = n-1; i >= 0; i--) {
        dp[i] = jobs[i][2];
        
        // find the maximum profit
        for(int j = i+1; j < n; j++) {
            if(jobs[i][1] <= jobs[j][0]) {
                dp[i] = max(dp[i], dp[j] + jobs[i][2]);
            }
        }
        
        res = max(res, dp[i]);
    }

    return res;
}

int main() {
    vector<vector<int>> jobs = {
        {1, 2, 50}, 
        {3, 5, 20}, 
        {6, 19, 100}, 
        {2, 100, 200}
    };
    cout << maxProfit(jobs);
    return 0;
}
Java
// Java program to implement 
// Weighted Job Scheduling
import java.util.*;

class GfG {
    // Function to find the maximum profit
    // from weighted job scheduling
    static int maxProfit(int[][] jobs) {
        int n = jobs.length;

        // Sort the jobs based on start time
        Arrays.sort(jobs, (a, b) -> 
                    Integer.compare(a[0], b[0]));

        // create a dp table
        int[] dp = new int[n];

        // stores the maximum profit
        int res = 0;

        // iterate over all the jobs
        for (int i = n - 1; i >= 0; i--) {
            dp[i] = jobs[i][2];

            // find the maximum profit
            for (int j = i + 1; j < n; j++) {
                if (jobs[i][1] <= jobs[j][0]) {
                    dp[i] = Math.max(dp[i], dp[j] + jobs[i][2]);
                }
            }

            res = Math.max(res, dp[i]);
        }

        return res;
    }

    public static void main(String[] args) {
        int[][] jobs = {
            {1, 2, 50}, 
            {3, 5, 20}, 
            {6, 19, 100}, 
            {2, 100, 200}
        };
        System.out.println(maxProfit(jobs));
    }
}
Python
# Python program to implement 
# Weighted Job Scheduling

# Function to find the maximum profit
# from weighted job scheduling
def maxProfit(jobs):
    n = len(jobs)

    # Sort the jobs based on start time
    jobs.sort()

    # create a dp table
    dp = [0] * n

    # stores the maximum profit
    res = 0

    # iterate over all the jobs
    for i in range(n - 1, -1, -1):
        dp[i] = jobs[i][2]

        # find the maximum profit
        for j in range(i + 1, n):
            if jobs[i][1] <= jobs[j][0]:
                dp[i] = max(dp[i], dp[j] + jobs[i][2])

        res = max(res, dp[i])

    return res

if __name__ == "__main__":
    jobs = [
        [1, 2, 50], 
        [3, 5, 20], 
        [6, 19, 100], 
        [2, 100, 200]
    ]
    print(maxProfit(jobs))
C#
// C# program to implement 
// Weighted Job Scheduling
using System;
using System.Linq;

class GfG {
    // Function to find the maximum profit
    // from weighted job scheduling
    static int MaxProfit(int[][] jobs) {
        int n = jobs.Length;

        // Sort the jobs based on start time
        Array.Sort(jobs, (a, b) => a[0].CompareTo(b[0]));

        // create a dp table
        int[] dp = new int[n];

        // stores the maximum profit
        int res = 0;

        // iterate over all the jobs
        for (int i = n - 1; i >= 0; i--) {
            dp[i] = jobs[i][2];

            // find the maximum profit
            for (int j = i + 1; j < n; j++) {
                if (jobs[i][1] <= jobs[j][0]) {
                    dp[i] = Math.Max(dp[i], dp[j] + jobs[i][2]);
                }
            }

            res = Math.Max(res, dp[i]);
        }

        return res;
    }

    static void Main(string[] args) {
        int[][] jobs = {
            new int[] {1, 2, 50},
            new int[] {3, 5, 20},
            new int[] {6, 19, 100},
            new int[] {2, 100, 200}
        };
        Console.WriteLine(MaxProfit(jobs));
    }
}
JavaScript
// JavaScript program to implement 
// Weighted Job Scheduling

// Function to find the maximum profit
// from weighted job scheduling
function maxProfit(jobs) {
    const n = jobs.length;

    // Sort the jobs based on start time
    jobs.sort((a, b) => a[0] - b[0]);

    // create a dp table
    const dp = new Array(n).fill(0);

    // stores the maximum profit
    let res = 0;

    // iterate over all the jobs
    for (let i = n - 1; i >= 0; i--) {
        dp[i] = jobs[i][2];

        // find the maximum profit
        for (let j = i + 1; j < n; j++) {
            if (jobs[i][1] <= jobs[j][0]) {
                dp[i] = Math.max(dp[i], dp[j] + jobs[i][2]);
            }
        }

        res = Math.max(res, dp[i]);
    }

    return res;
}

const jobs = [
    [1, 2, 50], 
    [3, 5, 20], 
    [6, 19, 100], 
    [2, 100, 200]
];
console.log(maxProfit(jobs));

Output
250

[Optimized Approach - 1] - Using Binary Search with Tabulation - O(n * log(n)) Time and O(n) Space

In above approach, for each index i, we are iterating from index i + 1 to n-1 to find the job with greater start time and maximum profit. As the jobs are sorted in ascending order, instead of iterating over all the elements we can perform binary search to find the index of the job with start time greater than or equal to end time of current job. Now dp[i] will store the maximum of dp[i] and dp[i+1]. And the final result will be stored in dp[0].

C++
// C++ program to implement 
// Weighted Job Scheduling 
#include <bits/stdc++.h>
using namespace std;

// Function to find the closest next job.
int findNextJob(int i, vector<vector<int>> &jobs) {
    int end = jobs[i][1];
    int ans = jobs.size();
    int s = i + 1, e = jobs.size() - 1;
    while (s <= e) {
        int mid = s + (e - s) / 2;
        if (jobs[mid][0] >= end) {
            ans = mid;
            e = mid - 1;
        }
        else {
            s = mid + 1;
        }
    }
    return ans;
}

// Function to find the maximum profit
// from weighted job scheduling
int maxProfit(vector<vector<int>> &jobs) {
    int n = jobs.size();

    // Sort the jobs based on start time
    sort(jobs.begin(), jobs.end());

    // create a dp table
    vector<int> dp(n);
    
    // iterate over all the jobs
    for(int i = n-1; i >= 0; i--) {
        dp[i] = jobs[i][2];
        
        // find the index of next job
        int next = findNextJob(i, jobs);

        // if next job exists
        if(next < n) {
            dp[i] += dp[next];
        }
        
        // store the maximum profit
        if(i < n-1)
            dp[i] = max(dp[i], dp[i+1]);
    }

    return dp[0];
}

int main() {
    vector<vector<int>> jobs = {
        {1, 2, 50}, 
        {3, 5, 20}, 
        {6, 19, 100}, 
        {2, 100, 200}
    };
    cout << maxProfit(jobs);
    return 0;
}
Java
// Java program to implement 
// Weighted Job Scheduling
import java.util.*;

class GfG {
    // Function to find the closest next job.
    static int findNextJob(int i, int[][] jobs) {
        int end = jobs[i][1];
        int ans = jobs.length;
        int s = i + 1, e = jobs.length - 1;
        while (s <= e) {
            int mid = s + (e - s) / 2;
            if (jobs[mid][0] >= end) {
                ans = mid;
                e = mid - 1;
            } else {
                s = mid + 1;
            }
        }
        return ans;
    }

    // Function to find the maximum profit
    // from weighted job scheduling
    static int maxProfit(int[][] jobs) {
        int n = jobs.length;

        // Sort the jobs based on start time
        Arrays.sort(jobs, (a, b) -> Integer.compare(a[0], b[0]));

        // create a dp table
        int[] dp = new int[n];

        // iterate over all the jobs
        for (int i = n - 1; i >= 0; i--) {
            dp[i] = jobs[i][2];

            // find the index of next job
            int next = findNextJob(i, jobs);

            // if next job exists
            if (next < n) {
                dp[i] += dp[next];
            }

            // store the maximum profit
            if (i < n - 1) {
                dp[i] = Math.max(dp[i], dp[i + 1]);
            }
        }

        return dp[0];
    }

    public static void main(String[] args) {
        int[][] jobs = {
            {1, 2, 50}, 
            {3, 5, 20}, 
            {6, 19, 100}, 
            {2, 100, 200}
        };
        System.out.println(maxProfit(jobs));
    }
}
Python
# Python program to implement 
# Weighted Job Scheduling

# Function to find the closest next job.
def findNextJob(i, jobs):
    end = jobs[i][1]
    ans = len(jobs)
    s, e = i + 1, len(jobs) - 1
    while s <= e:
        mid = s + (e - s) // 2
        if jobs[mid][0] >= end:
            ans = mid
            e = mid - 1
        else:
            s = mid + 1
    return ans

# Function to find the maximum profit
# from weighted job scheduling
def maxProfit(jobs):
    n = len(jobs)

    # Sort the jobs based on start time
    jobs.sort()

    # create a dp table
    dp = [0] * n

    # iterate over all the jobs
    for i in range(n - 1, -1, -1):
        dp[i] = jobs[i][2]

        # find the index of next job
        next = findNextJob(i, jobs)

        # if next job exists
        if next < n:
            dp[i] += dp[next]

        # store the maximum profit
        if i < n - 1:
            dp[i] = max(dp[i], dp[i + 1])

    return dp[0]

if __name__ == "__main__":
    jobs = [
        [1, 2, 50], 
        [3, 5, 20], 
        [6, 19, 100], 
        [2, 100, 200]
    ]
    print(maxProfit(jobs))
C#
// C# program to implement 
// Weighted Job Scheduling
using System;

class GfG {
    // Function to find the closest next job.
    static int FindNextJob(int i, int[][] jobs) {
        int end = jobs[i][1];
        int ans = jobs.Length;
        int s = i + 1, e = jobs.Length - 1;
        while (s <= e) {
            int mid = s + (e - s) / 2;
            if (jobs[mid][0] >= end) {
                ans = mid;
                e = mid - 1;
            } else {
                s = mid + 1;
            }
        }
        return ans;
    }

    // Function to find the maximum profit
    // from weighted job scheduling
    static int MaxProfit(int[][] jobs) {
        int n = jobs.Length;

        // Sort the jobs based on start time
        Array.Sort(jobs, (a, b) => a[0].CompareTo(b[0]));

        // create a dp table
        int[] dp = new int[n];

        // iterate over all the jobs
        for (int i = n - 1; i >= 0; i--) {
            dp[i] = jobs[i][2];

            // find the index of next job
            int next = FindNextJob(i, jobs);

            // if next job exists
            if (next < n) {
                dp[i] += dp[next];
            }

            // store the maximum profit
            if (i < n - 1) {
                dp[i] = Math.Max(dp[i], dp[i + 1]);
            }
        }

        return dp[0];
    }

    public static void Main(string[] args) {
        int[][] jobs = {
            new int[] {1, 2, 50}, 
            new int[] {3, 5, 20}, 
            new int[] {6, 19, 100}, 
            new int[] {2, 100, 200}
        };
        Console.WriteLine(MaxProfit(jobs));
    }
}
JavaScript
// JavaScript program to implement 
// Weighted Job Scheduling

// Function to find the closest next job.
function findNextJob(i, jobs) {
    const end = jobs[i][1];
    let ans = jobs.length;
    let s = i + 1, e = jobs.length - 1;
    while (s <= e) {
        const mid = s + Math.floor((e - s) / 2);
        if (jobs[mid][0] >= end) {
            ans = mid;
            e = mid - 1;
        } else {
            s = mid + 1;
        }
    }
    return ans;
}

// Function to find the maximum profit
// from weighted job scheduling
function maxProfit(jobs) {
    const n = jobs.length;

    // Sort the jobs based on start time
    jobs.sort((a, b) => a[0] - b[0]);

    // create a dp table
    const dp = new Array(n).fill(0);

    // iterate over all the jobs
    for (let i = n - 1; i >= 0; i--) {
        dp[i] = jobs[i][2];

        // find the index of next job
        const next = findNextJob(i, jobs);

        // if next job exists
        if (next < n) {
            dp[i] += dp[next];
        }

        // store the maximum profit
        if (i < n - 1) {
            dp[i] = Math.max(dp[i], dp[i + 1]);
        }
    }

    return dp[0];
}

// Main Function
const jobs = [
    [1, 2, 50], 
    [3, 5, 20], 
    [6, 19, 100], 
    [2, 100, 200]
];
console.log(maxProfit(jobs));

Output
250

[Optimized Approach - 2] - Using Binary Search with Memoization - O(n * log(n)) Time and O(n) Space

Similar to the above approach, binary search can be implemented on memoization approach to reduce the iterative calls. This approach has been discussed in artice Weighted Job Scheduling in O(n Log n) time.


Practice Tags :

Similar Reads