Skip to content

Instantly share code, notes, and snippets.

@oldmill1
Last active August 29, 2015 13:56
Show Gist options
  • Save oldmill1/8902458 to your computer and use it in GitHub Desktop.
Save oldmill1/8902458 to your computer and use it in GitHub Desktop.
Solves the denominations problems.
$denominations = array(0.5, 1, 2);
$total = 3.6;
for ($i = $total; $i > 0; $i = $i - $denominations[count($denominations)-1]) {
while ( $i < end($denominations) ){
unset($denominations[count($denominations)-1]);
}
if (empty($denominations)){
break;
}
$results[] = $denominations[count($denominations)-1];
}
if ($i > 0){
$results[] = $i;
}
/*
Returns:
Array
(
[0] => 2
[1] => 1
[2] => 0.5
[3] => 0.1
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment