<?php
$max = 111; //265;
$items = [50, 45, 30, 60, 70, 80];
function nextFitDecreasing(array $items, int $max): array
{
rsort($items);
$result = [];
foreach ($items as $item) {
if (!isset($pallet) || (array_sum($pallet) + $item) > $max) {
unset($pallet);
$result[] = &$pallet;
}
$pallet[] = $item;
}
return $result;
}
function firstFitDecreasing(array $items, int $max): array
{
rsort($items);
$result = [];
foreach ($items as $item) {
foreach ($result as &$pallet) {
if (array_sum($pallet) + $item <= $max) {
$pallet[] = $item;
continue 2;
}
}
$result[] = [$item];
}
return $result;
}
var_export(nextFitDecreasing($items, $max));
echo "\n---\n";
var_export(firstFitDecreasing($items, $max));
- Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
- array (
0 =>
array (
0 => 80,
),
1 =>
array (
0 => 70,
),
2 =>
array (
0 => 60,
1 => 50,
),
3 =>
array (
0 => 45,
1 => 30,
),
)
---
array (
0 =>
array (
0 => 80,
1 => 30,
),
1 =>
array (
0 => 70,
),
2 =>
array (
0 => 60,
1 => 50,
),
3 =>
array (
0 => 45,
),
)
preferences:
78.53 ms | 407 KiB | 5 Q