Program to find number of tasks can be finished with given conditions in Python

Suppose we have a list of tasks and another list of people. The tasks[i] determines the amount of strength required to perform the ith task. And the people[i] determines the amount of strength the ith person has. We need to find the maximum number of tasks that can be finished if one person can perform at most one task.

So, if the input is like tasks = [4, 3, 9, 15], people = [10, 5, 3, 2], then the output will be 3. The first person (strength 10) can perform task requiring 9, second person (strength 5) can perform task requiring 4, third person (strength 3) can perform task requiring 3, and fourth person (strength 2) can't perform any remaining tasks.

Algorithm

To solve this problem optimally, we use a greedy approach ?

  • Sort both tasks and people lists in ascending order
  • For each person, assign the easiest task they can complete
  • Use two pointers to track current person and current task
  • Count successful assignments

Example

def max_tasks_completed(tasks, people):
    # Sort both lists in ascending order
    tasks.sort()
    people.sort()
    
    completed_count = 0
    task_index = 0
    
    # For each person, find the easiest task they can do
    for person_strength in people:
        # Check remaining tasks starting from current position
        for j in range(task_index, len(tasks)):
            if person_strength >= tasks[j]:
                completed_count += 1
                task_index = j + 1  # Move to next available task
                break
            else:
                break  # Person can't do this task or any harder ones
    
    return completed_count

# Test the function
tasks = [4, 3, 9, 15]
people = [10, 5, 3, 2]
result = max_tasks_completed(tasks, people)
print(f"Maximum tasks that can be completed: {result}")
Maximum tasks that can be completed: 3

How It Works

Let's trace through the example step by step ?

def max_tasks_with_trace(tasks, people):
    print(f"Original tasks: {tasks}")
    print(f"Original people: {people}")
    
    tasks_sorted = sorted(tasks)
    people_sorted = sorted(people)
    
    print(f"Sorted tasks: {tasks_sorted}")
    print(f"Sorted people: {people_sorted}")
    print()
    
    completed_count = 0
    task_index = 0
    
    for i, person_strength in enumerate(people_sorted):
        print(f"Person {i+1} (strength {person_strength}):")
        assigned = False
        
        for j in range(task_index, len(tasks_sorted)):
            if person_strength >= tasks_sorted[j]:
                print(f"  ? Assigned task requiring {tasks_sorted[j]} strength")
                completed_count += 1
                task_index = j + 1
                assigned = True
                break
            else:
                break
        
        if not assigned:
            print("  ? No suitable task found")
        print()
    
    return completed_count

# Test with trace
tasks = [4, 3, 9, 15]
people = [10, 5, 3, 2]
result = max_tasks_with_trace(tasks, people)
print(f"Total completed tasks: {result}")
Original tasks: [4, 3, 9, 15]
Original people: [10, 5, 3, 2]
Sorted tasks: [3, 4, 9, 15]
Sorted people: [2, 3, 5, 10]

Person 1 (strength 2):
  ? No suitable task found

Person 2 (strength 3):
  ? Assigned task requiring 3 strength

Person 3 (strength 5):
  ? Assigned task requiring 4 strength

Person 4 (strength 10):
  ? Assigned task requiring 9 strength

Total completed tasks: 3

Key Points

  • Greedy Strategy: Always assign the easiest possible task to each person
  • Sorting: Makes it efficient to find suitable task assignments
  • Time Complexity: O(n log n + m log m) for sorting, where n and m are lengths of tasks and people
  • Space Complexity: O(1) if we can modify input lists, O(n + m) otherwise

Conclusion

This greedy algorithm efficiently maximizes task completion by sorting both lists and assigning the easiest available task to each person. The approach ensures optimal resource utilization with minimal computational complexity.

Updated on: 2026-03-25T10:36:17+05:30

415 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements