Program to find maximum credit we can get by finishing some assignments in python

Suppose we have two lists of the same size representing course assignments: deadlines and credits. Here deadlines[i] shows the deadline day for assignment i and credits[i] represents the amount of credits we get for assignment i. We have one day to complete an assignment, and it can be completed before or on the deadline day. We cannot do multiple assignments at the same time. We have to find the maximum credit we can gain by finishing some subset of assignments.

So, if the input is like deadlines = [1, 2, 2, 2] and credits = [4, 5, 6, 7], then the output will be 18, as we can complete assignment with credit 5 at day 1, complete assignment with credit 6 at day 2, and complete assignment with credit 7 at day 2.

Algorithm

To solve this, we will follow these steps:

  • Create pairs of deadlines and credits and sort them based on credits in descending order
  • If the list is empty, return 0
  • Create a result array of size (1 + maximum deadline) filled with 0 to track occupied days
  • Initialize answer to 0
  • For each pair (deadline, credit) in sorted list:
    • Try to schedule from deadline day backwards to day 0
    • If a day is free, assign the assignment and add credits to answer
  • Return the total answer

Example

class Solution:
    def solve(self, deadlines, credits):
        # Create pairs and sort by credits in descending order
        assignments = sorted(list(zip(deadlines, credits)), key=lambda x: x[1], reverse=True)
        
        if not assignments:
            return 0
        
        # Track occupied days (0 means free, 1 means occupied)
        occupied_days = [0] * (max(deadlines) + 1)
        total_credits = 0
        
        for deadline, credit in assignments:
            # Try to schedule from deadline backwards to day 0
            for day in range(deadline, -1, -1):
                if not occupied_days[day]:
                    occupied_days[day] = 1
                    total_credits += credit
                    break
        
        return total_credits

# Test the solution
ob = Solution()
deadlines = [1, 2, 2, 2]
credits = [4, 5, 6, 7]
result = ob.solve(deadlines, credits)
print(f"Maximum credits: {result}")
Maximum credits: 18

How It Works

The algorithm uses a greedy approach:

  • Sort assignments by credits in descending order to prioritize high-value assignments
  • For each assignment, try to schedule it as late as possible (closest to deadline) to leave room for other assignments
  • Use an array to track which days are already occupied

Step-by-Step Trace

For the example deadlines = [1, 2, 2, 2], credits = [4, 5, 6, 7]:

# After sorting by credits (descending): [(2,7), (2,6), (2,5), (1,4)]
# occupied_days = [0, 0, 0] initially

# Assignment (deadline=2, credit=7): Schedule at day 2 ? total = 7
# Assignment (deadline=2, credit=6): Schedule at day 1 ? total = 13  
# Assignment (deadline=2, credit=5): Schedule at day 0 ? total = 18
# Assignment (deadline=1, credit=4): No free days ? 1 ? skip

deadlines = [1, 2, 2, 2]
credits = [4, 5, 6, 7]

# Show the scheduling process
assignments = sorted(list(zip(deadlines, credits)), key=lambda x: x[1], reverse=True)
print("Sorted assignments (deadline, credit):", assignments)

occupied_days = [0] * (max(deadlines) + 1)
total_credits = 0

for deadline, credit in assignments:
    scheduled = False
    for day in range(deadline, -1, -1):
        if not occupied_days[day]:
            occupied_days[day] = 1
            total_credits += credit
            print(f"Scheduled assignment (deadline={deadline}, credit={credit}) on day {day}")
            print(f"Occupied days: {occupied_days}, Total credits: {total_credits}")
            scheduled = True
            break
    
    if not scheduled:
        print(f"Could not schedule assignment (deadline={deadline}, credit={credit})")
Sorted assignments (deadline, credit): [(2, 7), (2, 6), (2, 5), (1, 4)]
Scheduled assignment (deadline=2, credit=7) on day 2
Occupied days: [0, 0, 1], Total credits: 7
Scheduled assignment (deadline=2, credit=6) on day 1
Occupied days: [0, 1, 1], Total credits: 13
Scheduled assignment (deadline=2, credit=5) on day 0
Occupied days: [1, 1, 1], Total credits: 18
Could not schedule assignment (deadline=1, credit=4)

Conclusion

This greedy algorithm maximizes credits by prioritizing high-value assignments and scheduling them as late as possible within their deadlines. The time complexity is O(n²) where n is the number of assignments.

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

566 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements