<?php
function knapsackLight($value1, $weight1, $value2, $weight2, $maxW) {
$results[0] = $maxW >= $weight1 ? $value1 : 0;
$results[1] = $maxW >= $weight2 ? $value2 : 0;
$results[2] = $maxW >= $weight1 + $weight2 ? $value1 + $value2 : 0;
return max($results);
}
echo knapsackLight(10,5,6,4,8) . PHP_EOL; //10
echo knapsackLight(10,5,6,4,9) . PHP_EOL; //16
echo knapsackLight(5,3,7,4,6) . PHP_EOL; // 7
preferences:
23.94 ms | 404 KiB | 5 Q