Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Take as many of the biggest coin as possible and add them to your result. Add to the result by recursively calling your method on

Take as many of the biggest coin as possible and add them to your result.
Add to the result by recursively calling your method on the remaining amount,
leaving out the biggest coin, until the remainder is zero.
Once you have a working greedy version, talk with your partner about refactoring
this to 'makeBetterChange'. What's wrong with 'greedyMakeChange'?
Consider the case of 'greedyMakeChange(24,[10,7,1]. Because it takes as many
10 pieces as possible, 'greedyMakeChange` misses the correct answer of
[10,7,7](try it in node).
To 'makeBetterChange', we only take one coin at a time and never rule out
denominations that we've already used. This allows each coin to be available
each time we get a new remainder. By iterating over the denominations and
continuing to search for the best change, we assure that we test for
'non-greedy' uses of each denomination.
Discuss the following game plan and then work together to implement your
new method:
Iterate over each coin.
Grab only one of that one coin and recursively call 'makeBetterChange' on the
remainder using coins less than or equal to the current coin.
Add the single coin to the change returned by the recursive call. This will be
a possible solution, but maybe not the best one.
Keep track of the best solution and return it at the end.
N.B. Don't generate every possible permutation of coins and then compare them.
Remember that a permutation is not the same thing as a combination - we will
need to check every combination of coins that add up to our target, we just
don't want to check the same combination in different orders. If you get stuck
you can start by writing a solution that calculates and compares all of the
permutations without storing them in an array. Then go back and refactor your
solution so that it only calculates and compares all of the different
combinations.
function makeBetterChange(target, coins =[25,10,5,1]
// Your code here
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data, Mining, And Analytics Components Of Strategic Decision Making

Authors: Stephan Kudyba

1st Edition

1466568704, 9781466568709

More Books

Students also viewed these Databases questions