Skip to content

Instantly share code, notes, and snippets.

@thomaslevesque
Last active January 27, 2016 23:55
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 thomaslevesque/39090dbd0d4529f4e4b4 to your computer and use it in GitHub Desktop.
Save thomaslevesque/39090dbd0d4529f4e4b4 to your computer and use it in GitHub Desktop.
// Run in LINQPad
void Main()
{
var noteValues = new[] { 501, 251, 126, 64, 33 };
var notes = MakeAmount(1000, noteValues);
notes.GroupBy(v => v).Select(g => new { Value = g.Key, Count = g.Count() }).Dump();
}
IEnumerable<int> MakeAmount(int remaining, IEnumerable<int> noteValues)
{
var validNotes = noteValues.Where(v => v <= remaining).ToList();
foreach (var value in validNotes)
{
var list = new List<int> { value };
if (value == remaining)
return list;
var rest = MakeAmount(remaining - value, validNotes);
if (rest != null)
{
list.AddRange(rest);
return list;
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment