educative.io

Educative

What is a Greedy Approach? - Algorithms for Coding Interviews in Java

Refert the article discussing Greedy Approach here --> https://www.educative.io/courses/algorithms-coding-interviews-java/3j1R50KnNjQ

The real life example discusses a Piggy Bank with a capacity of 6 and the choice of items with (value,weight) combination being A(2,2), B(10,2) and C(100,2). The example chooses C+B+1/2A whereas the value can be maximized by taking 2C. My confusion why did we chose C+B+1/2A over 2C.

  • Is there any constraint that we need to have only one item of each type ?

Hi @Piyush_Mundra,
Yes, there is the constraint of only one item of each type and, the purpose of the example was to show the impact the order of selecting each item has on the total value. For example, selecting A before B would reduce the total value.

Regards