<?php
function knapsackLight($value1, $weight1, $value2, $weight2, $maxW) {
$results[0] = $maxW >= $weight1 && $value1 ? $value1 : null;
$results[1] = $maxW >= $weight2 && $value2 ? $value2 : null;
$results[2] = $maxW >= $weight1 + $weight2 && $value1 + $value2 ? $value1 + $value2 : null;
return max($results[0], $results[1], $results[2]);
}
var_dump(knapsackLight(10, 5, 6, 4, 8));
var_dump(knapsackLight(10, 5, 6, 5, 9));
preferences:
59.91 ms | 405 KiB | 5 Q