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. Finally, we have to find the 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, as the first person can perform task 9, second person can perform task 4, third person can perform task 3, and fourth person can't perform any tasks.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, tasks, people): tasks.sort() people.sort() ct=0 ind=0 for i in range(len(people)): for j in range(ind,len(tasks)): if people[i]>=tasks[j]: ct+=1 ind+=1 break else: break return ct ob = Solution() tasks = [4, 3, 9, 15] people = [10, 5, 3, 2] print(ob.solve(tasks, people))
[4, 3, 9, 15], [10, 5, 3, 2]