Program to get maximum profit by scheduling jobs in Python

Suppose we have a list of intervals where each interval contains three values [start, end, profit]. We can perform only one task at a time, and we have to find the maximum amount of profit we can get by scheduling non-overlapping jobs.

So, if the input is like intervals = [[1, 2, 100],[3, 5, 40],[6, 19, 150],[2, 100, 250]], then the output will be 350, as we can take these two intervals [1, 2, 100] and [2, 100, 250].

Algorithm Overview

To solve this problem, we will use dynamic programming with the following approach:

  • Create a dictionary to group jobs by their end times
  • Use an array to store maximum profit achievable up to each time point
  • For each end time, calculate the maximum profit by either taking or skipping the job
  • Return the maximum profit from the final array

Implementation

Let us see the following implementation to get better understanding ?

from collections import defaultdict

class Solution:
    def solve(self, intervals):
        d = defaultdict(list)
        n = 0
        
        # Group jobs by end time and find maximum end time
        for start, end, profit in intervals:
            if end > n:
                n = end
            d[end].append([start, profit])
        
        # Dynamic programming array to store maximum profit up to time i
        A = [0 for i in range(n + 1)]
        
        for end in range(len(A)):
            if end in d:
                for start, profit in d[end]:
                    # Take maximum of: current profit, new job profit + profit at start time, previous time profit
                    A[end] = max(A[end], A[start] + profit, A[end - 1])
            else:
                # No job ends at this time, carry forward previous profit
                A[end] = A[end - 1]
        
        return A[-1]

# Test the solution
ob = Solution()
intervals = [[1, 2, 100],[3, 5, 40],[6, 19, 150],[2, 100, 250]]
print(ob.solve(intervals))
350

How It Works

The algorithm works by building up the solution incrementally:

  • We group all jobs by their end times in a dictionary
  • We create a DP array where A[i] represents the maximum profit achievable up to time i
  • For each time point, we check if any jobs end at that time
  • If jobs end at time i, we calculate the maximum profit by considering whether to take each job or not
  • The final answer is stored in A[n] where n is the maximum end time

Example Walkthrough

For the input [[1, 2, 100],[3, 5, 40],[6, 19, 150],[2, 100, 250]]:

  • Jobs ending at time 2: [1, 2, 100] with profit 100
  • Jobs ending at time 5: [3, 5, 40] with profit 40
  • Jobs ending at time 19: [6, 19, 150] with profit 150
  • Jobs ending at time 100: [2, 100, 250] with profit 250

The optimal solution selects jobs [1, 2, 100] and [2, 100, 250] for a total profit of 350.

Conclusion

This dynamic programming approach efficiently solves the job scheduling problem by considering all possible combinations and selecting the one with maximum profit. The time complexity is O(n × m) where n is the maximum end time and m is the number of jobs.

Updated on: 2026-03-25T13:32:30+05:30

877 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements