- Related Questions & Answers
- Program to find length of longest word that can be formed from given letters in python
- Program to find number of bit 1 in the given number in Python
- Find maximum number that can be formed using digits of a given number in C++
- Find the largest number that can be formed with the given digits in C++
- Program to find the length of the longest, that can be made using given letters in Python
- Program to find maximum time to finish K tasks in Python
- Program to find minimum time to complete all tasks in python
- Find the ordering of tasks from given dependencies in C++
- Program to count number of palindromes of size k can be formed from the given string characters in Python
- Program to find the sum of all digits of given number in Python
- Program to find number of given operations required to reach Target in Python
- Program to find number of sublists we can partition so given list is sorted finally in python
- Program to find number of coins needed to make the changes with given set of coins in Python
- Program to find number of sublists whose sum is given target in python
- Program to find maximum number of coins we can collect in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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 −

- sort the list tasks, sort the list people
- ct:= 0, ind:= 0
- for i in range 0 to size of people, do
- for j in range ind to size of tasks, do
- if people[i] >= tasks[j], then
- ct := ct + 1
- ind := ind + 1
- come out from the loop

- otherwise,
- come out from the loop

- if people[i] >= tasks[j], then

- for j in range ind to size of tasks, do
- return ct

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]

3

Advertisements