3v4l.org

run code in 300+ PHP versions simultaneously
<?php $weight = 5678; $trucks = [1200, 600, 300]; $result = optimize($weight, $trucks); var_dump($result); $weight = 900; $trucks = [600, 350, 100]; $result = optimize($weight, $trucks); var_dump($result); $weight = 1300; $trucks = [1200, 600, 300]; $result = optimize($weight, $trucks); var_dump($result); function optimize(int $weight = 0, array $trucks = [], $sensitivity = 10): array { $weightStore = $weight; // cache weight // sort truck-type array (highest to lowest weight capacity). rsort($trucks); /* initialize truckCount at 0 for all types */ foreach ($trucks as $type) { $truckCount[$type] = 0; } foreach ($trucks as $type) { $capacity = $type; // truck capacity $exact = ($weight / $type); $round = (int) (floor($exact)); // whole trucks if ($exact >= 1) { $truckCount[$type] = $round; // whole trucks // adjust weight $weight = $weight - ($round * $type); } } // do we still have remaining weight if ($weight > 0) { rsort($trucks); foreach ($trucks as $type) { $ratio[$type] = $weight / $type; $max = max($ratio); } $type = array_search($max, $ratio); $truckCount[$type]++; $weight -= $type; // final weight adjustment } /* * use the ratio of truck capacities to identify * edge cases in results array: * e.g.: algorithm selected two trucks of 300 cap * where 1 of 600 cap would be more efficient. */ $ratioCap = []; foreach ($trucks as $key => $type) { if (isset($trucks[$key + 1])) { $ratioCap[$trucks[$key + 1]] = ($trucks[$key] / $trucks[$key + 1]); } } /* edge cases - force fewer trucks */ $sensitivity = ($sensitivity <= 0) ? 10 : $sensitivity; // set default if neg. or 0 foreach ($trucks as $cycle) { foreach ($truckCount as $type => $number) { foreach ($ratioCap as $key => $value) { if ($type == $key && $number >= (floor($value) / $sensitivity) && $number != 1) { $truckCount[$type] = 0; // group of smaller type trucks = 0 $truckCount[$type * $value] += 1; // the group of smaller trucks is moved into one larger truck } } } } /* truck capacity vs. weight transported */ $capacityUse = 0; foreach($truckCount as $type => $number) { $capacityUse += $type * $number; } $weight = $weightStore - $capacityUse; return ['trucks' => $truckCount, 'remaining weight' => $weight]; }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.4.130.0090.01117.58
8.4.120.0050.00620.62
8.4.110.0140.00618.79
8.4.100.0100.01117.91
8.4.90.0070.00218.13
8.4.80.0110.00918.12
8.4.70.0110.00919.45
8.4.60.0100.00720.29
8.4.50.0130.00919.25
8.4.40.0100.01019.62
8.4.30.0130.00622.49
8.4.20.0100.01020.64
8.4.10.0130.00622.34
8.3.260.0110.00816.78
8.3.250.0120.00718.66
8.3.240.0110.00916.82
8.3.230.0120.00716.52
8.3.220.0070.00318.68
8.3.210.0050.00316.70
8.3.200.0020.00716.68
8.3.190.0060.00418.83
8.3.180.0110.00820.77
8.3.170.0080.00018.88
8.3.160.0110.00718.49
8.3.150.0060.00320.91
8.3.140.0060.00316.54
8.3.130.0080.00016.69
8.3.120.0040.00420.69
8.3.110.0130.00320.94
8.3.100.0100.00024.06
8.3.90.0110.00026.77
8.3.80.0240.00417.36
8.3.70.0190.00817.40
8.3.60.0200.00317.34
8.3.50.0070.00617.67
8.3.40.0090.00818.20
8.3.30.0140.00318.15
8.3.20.0120.00219.12
8.3.10.0120.00219.79
8.3.00.0090.00620.01
8.2.290.0130.00620.80
8.2.280.0090.01020.39
8.2.270.0080.01117.42
8.2.260.0140.00419.27
8.2.250.0040.00416.75
8.2.240.0100.00018.94
8.2.230.0090.00622.58
8.2.220.0060.00337.54
8.2.210.0040.01426.77
8.2.200.0110.00317.25
8.2.190.0130.00616.55
8.2.180.0100.00716.55
8.2.170.0180.00318.42
8.2.160.0100.00620.34
8.2.150.0110.00221.46
8.2.140.0120.00121.08
8.2.130.0080.00521.76
8.2.120.0070.00621.77
8.2.110.0100.00419.36
8.2.100.0130.00418.57
8.2.90.0120.00417.73
8.2.80.0110.00417.76
8.2.70.0080.00517.95
8.2.60.0110.00317.83
8.2.50.0040.00917.86
8.2.40.0090.00418.52
8.2.30.0070.00718.40
8.2.20.0080.00517.82
8.2.10.0140.00718.38
8.2.00.0100.00518.29
8.1.330.0110.00817.74
8.1.320.0150.00516.26
8.1.310.0040.00418.54
8.1.300.0030.00616.22
8.1.290.0160.00523.36
8.1.280.0160.00521.02
8.1.270.0080.00718.82
8.1.260.0140.00021.92
8.1.250.0090.00622.73
8.1.240.0070.00619.78
8.1.230.0070.00720.04
8.1.220.0100.00317.57
8.1.210.0120.00418.06
8.1.200.0160.00517.30
8.1.190.0190.00617.19
8.1.180.0100.00717.72
8.1.170.0090.00318.04
8.1.160.0070.00518.22
8.1.150.0090.00518.09
8.1.140.0090.00618.11
8.1.130.0080.00618.74
8.1.120.0070.00517.52
8.1.110.0070.00717.45
8.1.100.0050.00717.46
8.1.90.0100.00217.46
8.1.80.0060.00417.37
8.1.70.0130.00117.48
8.1.60.0060.00617.52
8.1.50.0090.00417.49
8.1.40.0120.00417.43
8.1.30.0050.01017.60
8.1.20.0140.00217.45
8.1.10.0090.00517.57
8.1.00.0080.00617.48
8.0.300.0210.00418.01
8.0.290.0150.00916.63
8.0.280.0170.00617.52
8.0.270.0180.00516.95
8.0.260.0120.01018.48
8.0.250.0230.00516.77
8.0.240.0190.00516.86
8.0.230.0180.00516.75
8.0.220.0180.00316.62
8.0.210.0110.00916.61
8.0.200.0170.00216.87
8.0.190.0140.00716.80
8.0.180.0130.00716.88
8.0.170.0150.00816.81
8.0.160.0160.00416.73
8.0.150.0190.00416.76
8.0.140.0170.00616.68
8.0.130.0150.00714.97
8.0.120.0180.00616.78
8.0.110.0190.00516.70
8.0.100.0140.00916.68
8.0.90.0130.01316.76
8.0.80.0220.00916.82
8.0.70.0150.00816.67
8.0.60.0120.00416.73
8.0.50.0110.00516.72
8.0.30.0100.00817.01
8.0.20.0110.00916.95
8.0.10.0170.00616.95
8.0.00.0140.01117.01
7.4.330.0110.01016.09
7.4.320.0170.00516.41
7.4.300.0220.00016.52
7.4.290.0150.00716.50
7.4.280.0170.00516.53
7.4.270.0170.00516.36
7.4.260.0180.00314.85
7.4.250.0160.00516.47
7.4.240.0140.00316.48
7.4.230.0160.00616.31
7.4.220.0110.00616.45
7.4.210.0090.01116.55
7.4.200.0140.00916.45
7.4.190.0270.00616.23
7.4.180.0200.01016.50
7.4.160.0270.00316.63
7.4.150.0260.00316.38
7.4.140.0220.00416.09
7.4.130.0150.01316.26
7.4.120.0160.01416.36
7.4.110.0190.01016.32
7.4.100.0190.00716.53
7.4.90.0190.00316.38
7.4.80.0160.00417.76
7.4.70.0180.01016.45
7.4.60.0220.00416.44
7.4.50.0170.00916.37
7.4.40.0180.00816.48
7.4.30.0320.00616.25
7.4.20.0330.00416.25
7.4.10.0300.01016.25
7.4.00.0140.00715.47
7.3.330.0110.00316.20
7.3.320.0110.00014.83
7.3.310.0040.00716.42
7.3.300.0080.00516.35
7.3.290.0050.00616.34
7.3.280.0140.00216.34
7.3.270.0140.00216.13
7.3.260.0130.00616.47
7.3.250.0080.00816.13
7.3.240.0270.00616.25
7.3.230.0080.00916.20
7.3.220.0190.01316.25
7.3.210.0180.01316.40
7.3.200.0210.00716.14
7.3.190.0150.01016.33
7.3.180.0180.00616.30
7.3.170.0190.00516.46
7.3.160.0130.00616.25
7.3.150.0160.00716.00
7.3.140.0090.00916.00
7.3.130.0290.00716.13
7.3.120.0080.00815.31
7.3.110.0110.00515.10
7.3.100.0150.00615.10
7.3.90.0140.00715.18
7.3.80.0120.01015.01
7.3.70.0090.00715.04
7.3.60.0100.00715.08
7.3.50.0130.00515.19
7.3.40.0110.00715.20
7.3.30.0100.00715.03
7.3.20.0150.00416.43
7.3.10.0130.00716.37
7.3.00.0120.00716.43
7.2.340.0250.00416.25
7.2.330.0160.00816.62
7.2.320.0190.00716.50
7.2.310.0190.00816.51
7.2.300.0210.00516.45
7.2.290.0200.00816.28
7.2.280.0270.00916.13
7.2.270.0300.01016.00
7.2.260.0260.00316.38
7.2.250.0190.00715.50
7.2.240.0110.00815.49
7.2.230.0070.00915.28
7.2.220.0080.00715.42
7.2.210.0110.00615.38
7.2.200.0060.00815.39
7.2.190.0080.00715.37
7.2.180.0080.00615.39
7.2.170.0070.01015.50
7.2.160.0080.00515.28
7.2.150.0100.00416.74
7.2.140.0090.00716.84
7.2.130.0080.00816.86
7.2.120.0090.00716.81
7.2.110.0100.00816.75
7.2.100.0110.00716.75
7.2.90.0110.00916.82
7.2.80.0090.00816.85
7.2.70.0110.00616.76
7.2.60.0100.01116.80
7.2.50.0130.00616.69
7.2.40.0140.00816.55
7.2.30.0120.00816.89
7.2.20.0090.01116.71
7.2.10.0120.00716.64
7.2.00.0120.00916.77
7.1.330.0110.01116.28
7.1.320.0080.00715.76
7.1.310.0070.00715.66
7.1.300.0080.00515.62
7.1.290.0120.00615.61
7.1.280.0100.00715.60
7.1.270.0090.00715.63
7.1.260.0130.00615.63
7.1.250.0090.00815.64
7.1.240.0140.00616.46
7.1.230.0160.00716.30
7.1.220.0160.00716.43
7.1.210.0190.00516.34
7.1.200.0150.00616.42
7.1.190.0090.01216.27
7.1.180.0120.01016.33
7.1.170.0170.00316.27
7.1.160.0200.00316.27
7.1.150.0110.01116.55
7.1.140.0190.00516.48
7.1.130.0170.00916.57
7.1.120.0160.00616.36
7.1.110.0110.00916.49
7.1.100.0100.00516.44
7.1.90.0110.00316.25
7.1.80.0080.00416.47
7.1.70.0090.00616.38
7.1.60.0090.00716.46
7.1.50.0090.00516.42
7.1.40.0050.00916.32
7.1.30.0080.00616.48
7.1.20.0090.00716.29
7.1.10.0060.00916.41
7.1.00.0090.00516.46

preferences:
30.56 ms | 403 KiB | 5 Q