Skip to content

Instantly share code, notes, and snippets.

@NimaGhaedsharafi
Created May 13, 2021 08:40
Show Gist options
  • Save NimaGhaedsharafi/67ac69b1f6f1561c79d9411246888817 to your computer and use it in GitHub Desktop.
Save NimaGhaedsharafi/67ac69b1f6f1561c79d9411246888817 to your computer and use it in GitHub Desktop.
<?php
/**
* @param $items
* @return int
*/
function ks($size, $items)
{
if ($size == 0 || count($items) == 0) {
return 0;
}
if ($size < $items[0][0]) {
return ks($size, array_slice($items, 1));
}
return max(
$items[0][1] + ks($size - $items[0][0], array_slice($items, 1)),
ks($size, array_slice($items, 1))
);
}
echo ks(8, [
[1, 5],
[2, 3],
[4, 5],
[2, 4],
[5, 2],
]) . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment