Was it helpful?

Question

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

PythonServer Side ProgrammingProgramming

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
  • return ct

Let us see the following implementation to get better understanding −

Example

 Live Demo

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))

Input

[4, 3, 9, 15], [10, 5, 3, 2]

Output

3
raja
Published on 05-Oct-2020 11:02:42
Advertisements
Was it helpful?
Not affiliated with Tutorialspoint
scroll top