getValue() / $b['item']->getCost() <=> $a['item']->getValue() / $a['item']->getCost(); } /** * Fill the backpack with items * * @param array $items Items to fill the backpack with ['item' => Item, 'quantity' => ?] * @param BackpackInterface $backpack Backpack to fill * * @return BackpackInterface * * @since 1.0.0 */ public static function solve(array $items, BackpackInterface $backpack) : BackpackInterface { /* @phpstan-ignore-next-line */ \usort($items, ['self', 'continuousComparator']); $availableSpace = $backpack->getMaxCost(); foreach ($items as $item) { if ($availableSpace <= 0.0) { break; } $backpack->addItem( $item['item'], $quantity = \min($item['quantity'], $availableSpace / $item['item']->getCost()) ); $availableSpace -= $quantity * $item['item']->getCost(); } return $backpack; } }