Created
May 13, 2021 08:40
-
-
Save NimaGhaedsharafi/67ac69b1f6f1561c79d9411246888817 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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