Skip to content

Instantly share code, notes, and snippets.

@fupfin
Created December 23, 2014 05:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fupfin/af4db2c1cb34a829e758 to your computer and use it in GitHub Desktop.
Save fupfin/af4db2c1cb34a829e758 to your computer and use it in GitHub Desktop.
public class ChangeCounter {
int cents[] = { 50, 25, 10, 5, 1 };
public int count(int amount) {
return count(amount, 0);
}
private int count(int amount, int idx) {
if (amount == 0) {
return 1;
} else if (amount < 0 || idx >= cents.length) {
return 0;
}
return count(amount - cents[idx], idx) + count(amount, idx + 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment