Problem statement

https://binarysearch.com/problems/Task-Hare/

Solution

We need to use greedy strategy: start with biggest person and try to give him the biggest task.

Complexity

It is O(n log n + m log m) for time and O(m + n) for space.

Code

class Solution:
    def solve(self, tasks, people):
        people = sorted(people)[::-1]
        tasks = sorted(tasks)[::-1]
        i = j = ans = 0

        while i < len(tasks) and j < len(people):
            if people[j] >= tasks[i]:
                ans += 1
                j += 1
            i += 1

        return ans