Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DP algorithm for min-coin-change.js #200

Open
aldendmw opened this issue Jun 3, 2022 · 0 comments
Open

DP algorithm for min-coin-change.js #200

aldendmw opened this issue Jun 3, 2022 · 0 comments

Comments

@aldendmw
Copy link

aldendmw commented Jun 3, 2022

I guess there is a minor error, in the dynamic programming for min coin change in chapter 14, in the function makeChange there is a for block:

    for (let i = 0; i < coins.length; i++) {
      const coin = coins[i];
      newAmount = value - coin;
      if (newAmount >= 0) {
        newMin = makeChange(newAmount);
      }
      if (
        newAmount >= 0
        && (newMin.length < min.length - 1 || !min.length)
        && (newMin.length || !newAmount)
      ) {
        min = [coin].concat(newMin);
        // console.log('new Min ' + min + ' for ' + amount);
      }
    }

where in the commented line
console.log('new Min ' + min + ' for ' + amount);
I suppose it is for printing each cycle of the algorithm according to the published book, however
it should not be 'amount' or 'newAmount' I think, instead,
console.log('new Min ' + min + ' for ' + value);
will be in line with the out print for each loop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant