Answered step by step
Verified Expert Solution
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 Because it takes as many
pieces as possible, 'greedyMakeChange misses the correct answer of
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
'nongreedy' 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.
NB 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 makeBetterChangetarget coins
Your code here
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started