This is a technique that basically picks out the ā€œbestā€ option it can and hopes that it ends up a globally correct option The scenarios can vary for what choice would be the most optimal to satisfy the condition and for globally understanding

Question

You are given an array A of integers, where each element indicates the time a thing takes for completion. You want to calculate the maximum number of things that you can do in the limited time that you have.

  1. Sort the array A in ascending order
  2. Select each to-do item one by one.
  3. Add the time that it will take to complete that to-do into currentTime
  4. Add one to numberOfThings

Repeat this as long as theĀ currentTimeĀ is less than or equal toĀ T.

LetĀ A = {5, 3, 4, 2, 1}Ā andĀ T = 6 After sorting,Ā A = {1, 2, 3, 4, 5}

After the 1stĀ iteration:

  • currentTimeĀ = 1
  • numberOfThingsĀ = 1

After the 2ndĀ iteration:

  • currentTimeĀ is 1 + 2 = 3
  • numberOfThingsĀ = 2

After the 3rdĀ iteration:

  • currentTimeĀ is 3 + 3 = 6
  • numberOfThingsĀ = 3

After the 4thĀ iteration,Ā currentTimeĀ is 6 + 4 = 10, which is greater than T. Therefore, the answer is 3.